TeddyKiller Posted March 19, 2010 Share Posted March 19, 2010 I have two scripts. They both work on there own. One is cropping, one is resizing. (An image should always be cropped before resizing) As the work, I basically would need to set a variable at the end of script one, to pass it through to script two. That's all. It's a simple thing, however.. I can't do it. The image URL is gotten from the URL, the image has to run through both scripts and come out at the end as it normally would with one script. Cropping <?php $pic = $_GET['file']; list($current_width, $current_height) = getimagesize($pic); if($current_width > $current_height){ $crop_width = $current_height; $crop_height = $current_height; } elseif($current_height > $current_width){ $crop_width = $current_width; $crop_height = $current_width; ) $sum1 = $current_width - $crop_width; $left = $sum1 / 2; $sum2 = $current_height - $height; $top = $sum2 / 2; $current_image = imagecreatefromjpeg($pic); $canvas = imagecreatetruecolor($crop_width, $crop_height); imagecopy($canvas, $current_image, 0, 0, $left, $top, $current_width, $current_height); header('Content-type: image/jpeg'); imagejpeg($canvas,NULL,100); imagedestroy($current_image); imagedestroy($canvas); ?> Resizing <?php $pic = $_GET['file']; if (!isset($max_width)) $max_width = 90; if (!isset($max_height)) $max_height = 90; list($width, $height) = getimagesize($pic); $x_ratio = $max_width / $width; $y_ratio = $max_height / $height; if (($width <= $max_width) && ($height <= $max_height)){ $tn_width = $width; $tn_height = $height; } elseif (($x_ratio * $height) < $max_height){ $tn_height = ceil($x_ratio * $height); $tn_width = $max_width; } else { $tn_width = ceil($y_ratio * $width); $tn_height = $max_height; } $src = imagecreatefromjpeg($pic); $dst = imagecreatetruecolor($tn_width, $tn_height); imagecopyresampled($dst, $src, 0,0,0,0, $tn_width, $tn_height, $width,$height); header('Content-type: image/jpeg'); imagejpeg($dst,NULL,100); imagedestroy($src); imagedestroy($dst); ?> Can you help? Thanks Quote Link to comment Share on other sites More sharing options...
teamatomic Posted March 19, 2010 Share Posted March 19, 2010 Not exactly sure what you want cause you dont save just display but try this: at the start of #1 set a $timestamp then when finished save the output to $timestamp.jpg in #2 change the $pic to $pic = $timestamp.jpg remove the header from #2 in #1 include #2 at the end HTH Teamatomic Quote Link to comment Share on other sites More sharing options...
TeddyKiller Posted March 19, 2010 Author Share Posted March 19, 2010 Teamatonic, that can't be done. The images are random. So you'll end up with a folder of a billion images. I came up with this, although this doesn't output the image. http://horble.com/test/test.php Any help? Code below. <?php $pic = $_GET['file']; list($current_width, $current_height) = getimagesize($pic); if($current_width > $current_height){ $crop_width = $current_height; $crop_height = $current_height; } elseif($current_height > $current_width){ $crop_width = $current_width; $crop_height = $current_width; ) $sum1 = $current_width - $crop_width; $left = $sum1 / 2; $sum2 = $current_height - $height; $top = $sum2 / 2; $current_image = imagecreatefromjpeg($pic); $canvas = imagecreatetruecolor($crop_width, $crop_height); imagecopy($canvas, $current_image, 0, 0, $left, $top, $current_width, $current_height); header('Content-type: image/jpeg'); $cropimage = imagejpeg($canvas,NULL,100); imagedestroy($current_image); imagedestroy($canvas); $pic = $cropimage; if (!isset($max_width)) $max_width = 90; if (!isset($max_height)) $max_height = 90; list($width, $height) = getimagesize($pic); $x_ratio = $max_width / $width; $y_ratio = $max_height / $height; if (($width <= $max_width) && ($height <= $max_height)){ $tn_width = $width; $tn_height = $height; } elseif (($x_ratio * $height) < $max_height){ $tn_height = ceil($x_ratio * $height); $tn_width = $max_width; } else { $tn_width = ceil($y_ratio * $width); $tn_height = $max_height; } $src = imagecreatefromjpeg($pic); $dst = imagecreatetruecolor($tn_width, $tn_height); imagecopyresampled($dst, $src, 0,0,0,0, $tn_width, $tn_height, $width,$height); header('Content-type: image/jpeg'); imagejpeg($dst,NULL,100); imagedestroy($src); imagedestroy($dst); ?> Quote Link to comment Share on other sites More sharing options...
teamatomic Posted March 19, 2010 Share Posted March 19, 2010 I hate say it but you could bang your head against that for a year and it would not work that way. You have to drop the first image creation and the first thumb getsize as its not really a resource. Then use the crop_values instead of the width/height. I whacked out what you didnt need and changed it it use the crop values. It does however change the crop position so you will have to adjust that to suit where you want it. <?php $pic = './test.jpg'; list($current_width, $current_height) = getimagesize($pic); if($current_width > $current_height){ $crop_width = $current_height; $crop_height = $current_height; } elseif($current_height > $current_width){ $crop_width = $current_width; $crop_height = $current_width; } $sum1 = $current_width - $crop_width; $left = $sum1 / 2; $sum2 = $current_height - $height; $top = $sum2 / 2; if (!isset($max_width)) $max_width = 90; if (!isset($max_height)) $max_height = 90; $x_ratio = $max_width / $crop_width; $y_ratio = $max_height / $crop_height; if (($crop_width <= $max_width) && ($crop_height <= $max_height)){ $tn_width = $crop_width; $tn_height = $crop_height; } elseif (($x_ratio * $crop_height) < $max_height){ $tn_height = ceil($x_ratio * $crop_height); $tn_width = $max_width; } else { $tn_width = ceil($y_ratio * $crop_width); $tn_height = $max_height; } $src = imagecreatefromjpeg($pic);; $dst = imagecreatetruecolor($tn_width, $tn_height); imagecopyresampled($dst, $src, 0,0,0,0, $tn_width, $tn_height, $crop_width,$crop_height); header('Content-type: image/jpeg'); imagejpeg($dst,NULL,100); imagedestroy($src); imagedestroy($dst); ?> HTH Teamatomic Quote Link to comment Share on other sites More sharing options...
TeddyKiller Posted March 19, 2010 Author Share Posted March 19, 2010 The crop script centers the crop, and I've had a look.. and some of it I don't understand. $sum1 = $current_width - $crop_width; $left = $sum1 / 2; $sum2 = $current_height - $height; $top = $sum2 / 2; This is suppose to center the crop. $left and $top variables aren't being used. So where would they go. Can you center it for me? Also, possible comment the lines so I can understand them thanks Quote Link to comment Share on other sites More sharing options...
teamatomic Posted March 20, 2010 Share Posted March 20, 2010 You only need one coords to anchor the crop area casue we square the crop so we only anchor it to the longest axis. I just forgot to cut out that unused stuff. <?php $pic = './test.jpg'; list($width, $height) = getimagesize($pic); if($width > $height) // so we can square up the crop area { $long_s = $width; $short_s = $height; } else { $long_s = $height; $short_s = $width; } if (!isset($max_width)) $crop_width = 90; if (!isset($max_height)) $crop_height = 90; $dim_1=($long_s - $short_s)/2; //top left cords in original to anchor the crop area $src = imagecreatefromjpeg($pic); $dst = imagecreatetruecolor($crop_width, $crop_height); imagecopyresampled($dst, $src, 0,0,0,$dim_1, $crop_width, $crop_height,$short_s, $short_s ); header('Content-type: image/jpeg'); imagejpeg($dst,NULL,100); imagedestroy($src); imagedestroy($dst); ?> HTH Teamatomic Quote Link to comment Share on other sites More sharing options...
TeddyKiller Posted March 20, 2010 Author Share Posted March 20, 2010 http://horble.com/test/test.php One of the images doesn't load. I know it's a very.. very.. stupid image but it was for testing reasons only. Here is the image http://horble.com/test/profile/photos/test.jpg Any way to prevent this from happening? Other than that, the script works wonderfully. Quote Link to comment Share on other sites More sharing options...
teamatomic Posted March 20, 2010 Share Posted March 20, 2010 Yup. I didnt allow for a already equal axis. Change the width/height if to this if($width > $height) // so we can square up the crop area { $long_s = $width; $short_s = $height; } elseif($width < $height) { $long_s = $height; $short_s = $width; } else { $long_s = $height; $short_s = $width; } HTH Teamatomic Quote Link to comment Share on other sites More sharing options...
TeddyKiller Posted March 20, 2010 Author Share Posted March 20, 2010 Doesn't work. Would it be possible to add in a.. if($width > $height || $width < $height){ Do stuff here } There for, if the width and height is already square and equal, it would still go through resize? EDIT: That made stuff worse without having the variables in. Even with your method, for some reason that image just wont load. (The other images the one iwth a "No Photo" there perectly square anyway so.. So i'm imagining it's the graphics of the image? Quote Link to comment Share on other sites More sharing options...
Ruzzas Posted March 20, 2010 Share Posted March 20, 2010 Doesn't work. Would it be possible to add in a.. if($width > $height || $width < $height){ Do stuff here } There for, if the width and height is already square and equal, it would still go through resize? EDIT: That made stuff worse without having the variables in. Even with your method, for some reason that image just wont load. (The other images the one iwth a "No Photo" there perectly square anyway so.. So i'm imagining it's the graphics of the image? what the hell... Your checking if width is bigger than height and if height is bigger than width... You're gonna get a false for that. Quote Link to comment Share on other sites More sharing options...
TeddyKiller Posted March 20, 2010 Author Share Posted March 20, 2010 Actually, it has an "or" not an "and" Quote Link to comment Share on other sites More sharing options...
teamatomic Posted March 20, 2010 Share Posted March 20, 2010 @Ruzzas Wrong. It would always be true unless the axis are equal. @TeddyKiller What the little block of code does it to determine of the graphic is(think printing) page, landscape, or square. That way there is a starting point to lay in the crop so it is centered. With a square image it does not matter which side is declared. Which that block does just fine. I have tested it with equal axis images and it works as intended. If there is a problem it must lie within the image itself. Perhaps the image is a gif type, the script does not allow for non jpg images to be used. To do so would be easy, just use if's where there are jpg specific functions to select the proper type function and work from the original image extension. The final process could then save it to whatever type you want. HTH Teamatomic Quote Link to comment Share on other sites More sharing options...
TeddyKiller Posted March 20, 2010 Author Share Posted March 20, 2010 it was definately not a GIF. So probably just a problem in the image. I'm not using that image anymore as it was just testing. Thanks for the help Quote Link to comment 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.