Sometimes I need to not only resize the image, but also to change its size by removing unnecessary edges for new size. This function changes image size and if the height or width is too large after resizing then the edges are cut off from the center. Input image formats are jpeg, gif, png.
//resize and crop image by center function resize_crop_image($max_width, $max_height, $source_file, $dst_dir, $quality = 80){ $imgsize = getimagesize($source_file); $width = $imgsize[0]; $height = $imgsize[1]; $mime = $imgsize['mime']; switch($mime){ case 'image/gif': $image_create = "imagecreatefromgif"; $image = "imagegif"; break; case 'image/png': $image_create = "imagecreatefrompng"; $image = "imagepng"; $quality = 7; break; case 'image/jpeg': $image_create = "imagecreatefromjpeg"; $image = "imagejpeg"; $quality = 80; break; default: return false; break; } $dst_img = imagecreatetruecolor($max_width, $max_height); $src_img = $image_create($source_file); $width_new = $height * $max_width / $max_height; $height_new = $width * $max_height / $max_width; //if the new width is greater than the actual width of the image, then the height is too large and the rest cut off, or vice versa if($width_new > $width){ //cut point by height $h_point = (($height - $height_new) / 2); //copy image imagecopyresampled($dst_img, $src_img, 0, 0, 0, $h_point, $max_width, $max_height, $width, $height_new); }else{ //cut point by width $w_point = (($width - $width_new) / 2); imagecopyresampled($dst_img, $src_img, 0, 0, $w_point, 0, $max_width, $max_height, $width_new, $height); } $image($dst_img, $dst_dir, $quality); if($dst_img)imagedestroy($dst_img); if($src_img)imagedestroy($src_img); } //usage example resize_crop_image(100, 100, "test.jpg", "test.jpg");
It's just what I was looking for!! Thank you very much!!
You are welcome! I just found there is not set quality for jpeg, so I added $quality = 80; in line 23, else imagejpeg function will use default value 75.
good!
Thanks a lot !
Works great from scratch. Adjusted it for my project with success. 🙂
Thanx buddy for this! haven't implemented it yet, but seems as others have had great success with it.
Thanks for sharing - highly appreciated!
Good JOB
Just perfect!
Works perfect, thank you
I've made a JQuery/HTML5 image preview before upload which can see on JSFiddle here: http://jsfiddle.net/qohtqje5/
If possible, how would I call:
resize_crop_image(100, 100, "test.jpg", "test.jpg");
replacing "test.jpg" with the users local image then upload to img/thumbs/ please?
To call resize_crop_image you need upload image to server so php can access image.
Thank you Aleksandras, I've not set my image to upload to the temp location $_FILES["file"]["tmp_name"]. My full script can be seen here with a password set to *********.
How do I call upon your script to crop to a square then resize to 120x120px?
I tired adding your script then the following which did not work:resize_crop_image(120, 120, $_FILES["file"]["tmp_name"], "../Desktop/IMG/BananzaNews/Thumbs/");
$dst_dir must be file name or path to file, but you set just directory. You can use functon just after image uploaded and moved:
How would I modify your script to be implemented in this part;
Thank you for your help so far, I am very appreciative! This is my most advanced suite so far for my charity. My first database, submitting, reading and whatnot. It'll click with me one day! 🙂
Oh and how do I wrap code nicely like you have done? I tried wrapping in code tags but it didn't seem to work?
You can make thumb just after expression:
I wrap code with [ php ]code[ /php ] without spaces
Well at long last the uploaded image is now in fact 120x120px! I uploaded this image and the outcome can be found here however it did not keep the transparency...
how to keep image max height with original instead of fixed defined image height width ?
Get original image height and send to function:
Thanks for function,,it really helpful for me...
improve function
Thanks for transparency support!
I Really Like Your Script, thnx for it
but what i need is after Cropping image just display to browser, not save to disk..???
is it Possible.. How?
Yes, it is possible. Just add header for each image format and for filename ($dst_dir) use null, so this line
for jpg image change to:
You can add header selection for each image format in switch statement or echo it before function.
Thanks ... saved me hours of work
Perfect!
I love you, guy!
Thank you !! you saved my day
Muchas gracias por compartir. Funciona perfecto!
Great.
Work perfectly.
Thank you!!!
Todo perfecto a nivel de navegador y visiblemente funciona muy bien.
Sin embargo en PHP 8.3 en el error_log me sale "obsoleto": La conversión implícita de float (nro) a int pierde precisión en (...)
La linea de error da exactamente en: imagecopyresampled();
Como se podria solucionar eso? No me agrada que el servidor me genere los error_log's. Aunque aparentemente me funcione perfecto.
Solucionado:
$h_point = (($height - $height_new) / 2);
$h_point = intval($h_point);
y en
$w_point = (($width - $width_new) / 2);
$w_point = intval($w_point);