madk Posted May 22, 2008 Share Posted May 22, 2008 Hello all, I'm trying to modify my thumbnail function to crop my thumbnails to 150 x 150. I don't want to resize the image I just want to crop off the extra area to create the square. I can't wrap my brain around all of the variables of imagecopyresampled to accomplish this. Could someone please guide me. I appreciate it. function thumbJPG($jpgFile) { // Get dimensions of original list($width_orig, $height_orig) = getimagesize($jpgFile); if($width_orig > $height_orig) { $height = 150; $width = (int) (($height / $height_orig) * $height_orig); $image_p = imagecreatetruecolor($width, $height); $image = imagecreatefromjpeg($jpgFile); imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig); } else { $width = 150; $height = (int) (($width / $width_orig) * $height_orig); $image_p = imagecreatetruecolor($width, $height); $image = imagecreatefromjpeg($jpgFile); imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig); } // Output imagejpeg($image_p, $jpgFile, 100); } Link to comment https://forums.phpfreaks.com/topic/106779-modify-my-function-to-resize-then-crop-a-thumbnail/ Share on other sites More sharing options...
madk Posted May 22, 2008 Author Share Posted May 22, 2008 SOLVED function thumbJPG($jpgFile) { // Get dimensions of original list($width_orig, $height_orig) = getimagesize($jpgFile); $image_p = imagecreatetruecolor(150, 150); $image = imagecreatefromjpeg($jpgFile); if($width_orig > $height_orig) { $off_w = ($width_orig-$height_orig)/2; $off_h = 0; $width_orig = $height_orig; } elseif($height_orig > $width_orig) { $off_w = 0; $off_h = ($height_orig-$width_orig)/2; $height_orig = $width_orig; } else { $off_w = 0; $off_h = 0; } imagecopyresampled($image_p, $image, 0, 0, $off_w, $off_h, 150, 150, $width_orig, $height_orig); // Output imagejpeg($image_p, $jpgFile, 100); } Link to comment https://forums.phpfreaks.com/topic/106779-modify-my-function-to-resize-then-crop-a-thumbnail/#findComment-547363 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.