jarvis Posted December 14, 2009 Share Posted December 14, 2009 Hi all, I use the following script to resize my images to either 150 high or wide and 800 high or wide What I need to do, is set the thumbnail to be 150 x 150 and take a crop of the main image and resize the main to 800px high or wide. how can I alter the below to do this? <?php function saveThumb($gal,$fn) { define("MAX_XY_SMALL", 150); // maximum width or height of thumbnail image define("MAX_XY_LARGE", 800); // maximum width or height of thumbnail image $fpath = "categories/$gal/$fn"; //print $fpath."\n"; /* GetImageSize returns an array: * $info[0] - horizontal size of image in pixels * $info[1] - vertical size of image in pixels * $info[2] - type of image (1=GIF, 2=JPEG, 3=PNG) */ $info = GetImageSize("$fpath"); //print_r($info); // do we need to resize image? if ($info[0] > MAX_XY_SMALL || $info[1] > MAX_XY_SMALL) { // is image landscape? if ($info[0] >= $info[1]) { $width = MAX_XY_SMALL; $height = $info[1]*MAX_XY_SMALL/$info[0]; // or portrait? } else { $height = MAX_XY_SMALL; $width = $info[0]*MAX_XY_SMALL/$info[1]; } } else { // use original dimensions $width = $info[0]; $height = $info[1]; } if ($info[0] > MAX_XY_LARGE || $info[1] > MAX_XY_LARGE) { // is image landscape? if ($info[0] >= $info[1]) { $width_large = MAX_XY_LARGE; $height_large = $info[1]*MAX_XY_LARGE/$info[0]; // or portrait? } else { $height_large = MAX_XY_LARGE; $width_large = $info[0]*MAX_XY_LARGE/$info[1]; } } else { // use original dimensions $width_large = $info[0]; $height_large = $info[1]; } // create new thumbnail image //echo "<br>$width - $height<br>"; $im = ImageCreateTrueColor($width, $height); $im_large = ImageCreateTrueColor($width_large, $height_large); /* determine image type and load original image. Notice new tests for image * types. The GD extension has changed a lot over the years, dropping GIFs * and adding PNG support. By testing, we avoid trying to process an image * PHP can't deal with */ $im_big = ""; // default is no image switch ($info[2]) { case 1 : if (ImageTypes() & IMG_GIF) // test for GIF support $im_big = ImageCreateFromGIF("$fpath"); break; case 2 : if (ImageTypes() & IMG_JPG) // test for JPEG support $im_big = ImageCreateFromJPEG("$fpath"); break; case 3 : if (ImageTypes() & IMG_PNG) // test for PNG support $im_big = ImageCreateFromPNG("$fpath"); break; case 4 : if (ImageTypes() & IMG_BMP) // test for BMP support $im_big = ImageCreateFromBMP("$fpath"); break; } if ($im_big) { /* resize original image into thumbnail - see PHP Manual for details on * the arguements ImageCopyResized takes */ ImageCopyResized($im,$im_big,0,0,0,0,$width,$height,$info[0],$info[1]); ImageCopyResized($im_large,$im_big,0,0,0,0,$width_large,$height_large,$info[0],$info[1]); } else { // couldn't load original image, so generate 'no thumbnail' image instead $width = 150; $height = 150; $im = ImageCreate($width, $height); // create a blank image $bgc = ImageColorAllocate($im, 0, 0, 0); // background color = black $tc = ImageColorAllocate($im, 255, 255, 255); // text color = white ImageFilledRectangle($im, 0, 0, $width, $height, $bgc); // draw rectangle ImageString($im, 3, 9, 36, "No thumbnail", $tc); // add text ImageString($im, 3, 17, 48, "available", $tc); } /* save our image in thumb directory - if the second argument is left out, * the image gets sent to the browser, as in thumbnail.php */ ImageJPEG($im, "categories/".$gal."/t_".$fn); ImageJPEG($im_large, "categories/".$gal."/".$fn); // clean up our working images ImageDestroy($im_big); ImageDestroy($im); ImageDestroy($im_large); } ?> Thanks in advanced! Quote Link to comment https://forums.phpfreaks.com/topic/185120-set-image-resize-to-a-square-and-crop/ Share on other sites More sharing options...
jarvis Posted December 15, 2009 Author Share Posted December 15, 2009 Hi All, Following on fropm the above, do I need to run the script in several parts? - check image size - resize for large - create large - crop - resize - create thumb Or is it easier than that? thanks Quote Link to comment https://forums.phpfreaks.com/topic/185120-set-image-resize-to-a-square-and-crop/#findComment-977628 Share on other sites More sharing options...
jarvis Posted December 15, 2009 Author Share Posted December 15, 2009 Sorry to bump but I'm struggling with this, any helps much appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/185120-set-image-resize-to-a-square-and-crop/#findComment-977904 Share on other sites More sharing options...
calmchess Posted December 15, 2009 Share Posted December 15, 2009 function imagesdir($picref,$filein,$fileout,$imagethumbsize_w,$imagethumbsize_h,$red,$green,$blue,$white,$username,$password,$dbase,$email,$fname,$lname,$bday,$link,$gender,$id,$form0,$form1,$form2,$form3,$form4,$refNum1){ $target = "../Images/".$picref; $target = $target.time().basename($picref) ; move_uploaded_file("../sourcepics/".time().$picref,$target); $dbase = basename("../sourcepics/".$picref) ; //echo "The picture has been uploaded"; } $filein = '../sourcepics/'.$dbase; // File in $fileout = '../Images/'.time().$dbase; // Fileout - optional $imagethumbsize_w = 204; // thumbnail size (area cropped in middle of image) $imagethumbsize_h = 151; // thumbnail size (area cropped in middle of image) { // Get new dimensions $format = 'image/jpg'; if(preg_match("/.jpg/i", "$filein")) { $format = 'image/jpg'; } if (preg_match("/.gif/i", "$filein")) { $format = 'image/gif'; } if(preg_match("/.png/i", "$filein")) { $format = 'image/png'; } switch($format) { case 'image/jpg': $image = imagecreatefromjpeg($filein); break; case 'image/gif'; $image = imagecreatefromgif($filein); break; case 'image/png': $image = imagecreatefrompng($filein); break; } $width = $imagethumbsize_w ; $height = $imagethumbsize_h ; list($width_orig, $height_orig) = getimagesize($filein); if ($width_orig < $height_orig) { $height = ($imagethumbsize_w / $width_orig) * $height_orig; } else { $width = ($imagethumbsize_h / $height_orig) * $width_orig; } if ($width < $imagethumbsize_w) //if the width is smaller than supplied thumbnail size { $width = $imagethumbsize_w; $height = ($imagethumbsize_w/ $width_orig) * $height_orig;; } if ($height < $imagethumbsize_h) //if the height is smaller than supplied thumbnail size { $height = $imagethumbsize_h; $width = ($imagethumbsize_h / $height_orig) * $width_orig; } $thumb = imagecreatetruecolor($width , $height); $bgcolor = imagecolorallocate($thumb, $red, $green, $blue); ImageFilledRectangle($thumb, 0, 0, $width, $height, $bgcolor); imagealphablending($thumb, true); imagecopyresampled($thumb, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig); $thumb2 = imagecreatetruecolor($imagethumbsize_w , $imagethumbsize_h); // true color for best quality $bgcolor = imagecolorallocate($thumb2, $red, $green, $blue); ImageFilledRectangle($thumb2, 0, 0, $imagethumbsize_w , $imagethumbsize_h , $white); imagealphablending($thumb2, true); $w1 =($width/2) - ($imagethumbsize_w/2); $h1 = ($height/2) - ($imagethumbsize_h/2); imagecopyresampled($thumb2, $thumb, 0,0, $w1, $h1, $imagethumbsize_w , $imagethumbsize_h ,$imagethumbsize_w, $imagethumbsize_h); // Output //header('Content-type: image/gif'); //imagegif($thumb); //output to browser first image when testing if ($fileout !=""){ imagejpeg($thumb2, $fileout); //write to file Quote Link to comment https://forums.phpfreaks.com/topic/185120-set-image-resize-to-a-square-and-crop/#findComment-978029 Share on other sites More sharing options...
calmchess Posted December 15, 2009 Share Posted December 15, 2009 not sure if mine does anymore than yours but it does crop from the center. Quote Link to comment https://forums.phpfreaks.com/topic/185120-set-image-resize-to-a-square-and-crop/#findComment-978030 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.