jond Posted November 26, 2009 Share Posted November 26, 2009 Hi, I'm trying to resize and then crop an image. I found the following code for resizing an image. <?php // The file $filename = $_GET['src']; // Set a maximum height and width $width = 256; $height = 1000; // Content type header('Content-type: image/jpeg'); // Get new dimensions list($width_orig, $height_orig) = getimagesize($filename); $ratio_orig = $width_orig/$height_orig; if ($width/$height > $ratio_orig) { $width = $height*$ratio_orig; } else { $height = $width/$ratio_orig; } // Resample $image_p = imagecreatetruecolor($width, $height); $image = imagecreatefromjpeg($filename); imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig); imagejpeg($image_p, null, 100); ?> I thought that I could put the result into a variable like $resized = imagejpeg($image_p, null, 100); to then use with this code for cropping the image. Replacing the $_GET with the $resized variable <?php $x=0; $y=0; $filename=$_GET['src']; header('Content-type:image/jpg'); header('Content-Disposition: attachment;filename='.$src); list($width, $height, $type, $attr) = getimagesize($filename); $w= $width ; $h= 86; $image=imagecreatefromjpeg($filename); $crop=imagecreatetruecolor($w,$h); imagecopy($crop,$image,0,0,$x,$y,$w,$h); imagejpeg($crop); ?> It seems though that I am wrong. Could someone help me with this? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/183061-resizing-and-then-cropping-an-image/ Share on other sites More sharing options...
MadTechie Posted November 26, 2009 Share Posted November 26, 2009 As your not writing the image back to a file the second code won't work, try this instead replace imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig); imagejpeg($image_p, null, 100); ?> <?php $x=0; $y=0; $height= 86; imagecopyresampled($image_p, $image, 0, 0, $x, $y, $width, $height, $width_orig, $height_orig); imagejpeg($image_p); ?> Quote Link to comment https://forums.phpfreaks.com/topic/183061-resizing-and-then-cropping-an-image/#findComment-966149 Share on other sites More sharing options...
jond Posted November 28, 2009 Author Share Posted November 28, 2009 Thanks MadTechie! I took what you said and fixed it a little to get the result I wanted. For anyone else interested this is the code. <?php // The file $filename = $_GET['src']; // Set a maximum height and width $width = 256; $height = 1000; // Content type header('Content-type: image/jpeg'); // Get new dimensions list($width_orig, $height_orig) = getimagesize($filename); $ratio_orig = $width_orig/$height_orig; if ($width/$height > $ratio_orig) { $width = $height*$ratio_orig; } else { $height = $width/$ratio_orig; } $height = 83; // Resample $image_p = imagecreatetruecolor($width, $height); $image = imagecreatefromjpeg($filename); $x=0; $y=0; $width=256; $height_orig = $height * $ratio_orig; imagecopyresampled($image_p, $image, 0, 0, $x, $y, $width, $height, $width_orig, $height_orig); imagejpeg($image_p); ?> Quote Link to comment https://forums.phpfreaks.com/topic/183061-resizing-and-then-cropping-an-image/#findComment-966968 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.