I am trying to create an image uploading system but came across a problem. The system uploads the inserted image and creates a thumbnail.
The user is allowed to give the thumbnail width and height. When the image is lets say 200px by 100px and the user said the thumbnail width to be 10px and the height to be 20px, the system has to stretch the image which I do not want to happen. I want the system to chop a part of the image with the function Imagick, in order to obtain a normal thumbnail and image when an image goes trough the script.
The function is used like this:
chopImage ( width of the chopped area , height of the chopped area , x coordinate of topleft corner of chopped area , y coordinate of topleft corner of chopped area )
Standart configurations of php uploading system:
$image = $_FILES["image"]["name"];
$imgtemp = $_FILES["image"]["tmp_name"];
$imgtype = $_FILES["image"]["type"];
$imgsize = $_FILES["image"]["size"];
if(!$image){
if($image_enabled ==2){
$errors[] = "You didn't selected an image to upload";
}
} else {
if( $imgtype == 'image/jpeg' ){ $filetype= '.jpg'; }else{ $filetype= str_replace ( 'image/', '', $imgtype ); }
$path= 'images/' . md5( rand( 0, 1000 ) . rand( 0, 1000 ) . rand( 0, 1000 ) . rand( 0, 1000 ) ) . '.jpg';
$thumb_path= 'images/thumb_' . md5( rand( 0, 1000 ) . rand( 0, 1000 ) . rand( 0, 1000 ) . rand( 0, 1000 ) ) . '.jpg';
$imgsize2= getimagesize( $imgtemp );
$width= $imgsize2[0];
$height= $imgsize2[1];
$maxwidth= 1281;
$maxheight= 721;
$allowed= array( 'image/png', 'image/jpeg', 'image/gif', );
if( in_array( $imgtype, $allowed ) ){
if( $width < $maxwidth && $height < $maxheight ){
if( $imgsize < 5242880 ){
The part I am stucked with:
if(!isEmpty($image_thumbheight) || !isEmpty($image_thumbwidth)){
switch( $imgtype ){
case 'image/gif';
$img= imagecreatefromgif( $imgtemp );
$img_thumb= imagecreatefromgif( $thumb );
header("Content-type: image/gif");
$image_crop = new Imagick("$image");
$image_crop->cropImage( $image_thumbwidth,$image_thumbheight, 0,$height );
$image_crop->writeImage($thumb);
imagegif( $thumb, $thumb_path );
break;
case 'image/png';
$img= imagecreatefrompng( $imgtemp );
header("Content-type: image/png");
$image_crop = new Imagick("$imgtemp");
$image_crop->chopImage( $image_thumbwidth,$image_thumbheight, 0,$height );
$image_crop->writeImage($thumb);
imagepng( $thumb, $thumb_path );
break;
case 'image/jpeg';
$img= imagecreatefromjpeg( $imgtemp );
$img_thumb= imagecreatefromjpeg( $thumb );
header("Content-type: image/jpeg");
$image_crop = new Imagick("$image");
$image_crop->chopImage( $image_thumbwidth,$image_thumbheight, 0,$height );
$image_crop->writeImage($thumb_path);
imagejpeg( $thumb, $thumb_path );
break;
}
move_uploaded_file( $imgtemp, $path );
echo "Image is successfully uploaded.";
}
When I run this script it only uploads the normal image. The thumbnail is not uploaded. The imagick function is not used very often I reckon, because couldn't find any upload tutorials on the internet with the usage of the imagick function. Can someone help me out?