rofl90 Posted April 7, 2008 Share Posted April 7, 2008 I found this and it works but to save server resources, is there any possible way to turn it into a file such as thumb.php and have parameters such as thumb.php?width=100&height=100 and have it output the image instead of just saving it to file. Heres the code: <?php // Set our crop dimensions. $width = 100; $height = 75; // Get dimensions of existing image $dimensions = getimagesize('path/to/image'); // Prepare canvas $canvas = imagecreatetruecolor($width,$height); $piece = imagecreatefromjpeg('path/to/image'); // Prepare image resizing and crop -- Center crop location $newwidth = $dimensions[0] / 2; $newheight = $dimensions[1] / 2; $cropLeft = ($newwidth/2) - ($width/2); $cropHeight = ($newheight/2) - ($height/2); // Generate the cropped image imagecopyresized($canvas, $piece, 0,0, $cropLeft, $cropHeight, $width, $height, $newwidth, $newheight); // Write image or fail if (imagejpeg($canvas,'path/to/save/file',90)) { echo 'Image crop successful'; } else { echo 'Image crop failed'; } // Clean-up imagedestroy($canvas); imagedestroy($piece); ?> Link to comment https://forums.phpfreaks.com/topic/100028-gd-cropping/ Share on other sites More sharing options...
rofl90 Posted April 7, 2008 Author Share Posted April 7, 2008 bump Link to comment https://forums.phpfreaks.com/topic/100028-gd-cropping/#findComment-511590 Share on other sites More sharing options...
tippy_102 Posted April 8, 2008 Share Posted April 8, 2008 If you want to display the file instead of saving it.... header("Content-type: image/png"); imagepng($canvas); Link to comment https://forums.phpfreaks.com/topic/100028-gd-cropping/#findComment-511750 Share on other sites More sharing options...
rofl90 Posted April 8, 2008 Author Share Posted April 8, 2008 I get a barrage of errors... Warning: getimagesize(http://www.x.com/uploads/charlie.gif) [function.getimagesize]: failed to open stream: HTTP request failed! HTTP/1.1 401 Authorization Required in /home/x/public_html/thumb.php on line 7 Warning: imagecreatefromjpeg(http://www.x.com/uploads/charlie.gif) [function.imagecreatefromjpeg]: failed to open stream: HTTP request failed! HTTP/1.1 401 Authorization Required in /home/x/public_html/thumb.php on line 10 Warning: imagecopyresized(): supplied argument is not a valid Image resource in /home/x/public_html/thumb.php on line 17 Warning: Cannot modify header information - headers already sent by (output started at /home/charliek/public_html/thumb.php:7) in /home/x/public_html/thumb.php on line 19 ‰PNG ��� IHDR���d���d���ÿ€���4IDATxœíÁ ��� ÷Om7 �����������������������������~u”�¨Pò9����IEND®B`‚ Link to comment https://forums.phpfreaks.com/topic/100028-gd-cropping/#findComment-511943 Share on other sites More sharing options...
rofl90 Posted April 8, 2008 Author Share Posted April 8, 2008 bump/ Link to comment https://forums.phpfreaks.com/topic/100028-gd-cropping/#findComment-512399 Share on other sites More sharing options...
tippy_102 Posted April 8, 2008 Share Posted April 8, 2008 Check that your path is correct for getimagesize - $dimensions = getimagesize("http://www.x.com/uploads/charlie.gif"); If you're using a gif file, you have to use imagecreatefromgif Link to comment https://forums.phpfreaks.com/topic/100028-gd-cropping/#findComment-512446 Share on other sites More sharing options...
obsidian Posted April 8, 2008 Share Posted April 8, 2008 Also, check to see that you have the fopen URL wrappers turned on. If not, you won't be able to use getimagesize (or any other like functions) to open the source location via a URL. Link to comment https://forums.phpfreaks.com/topic/100028-gd-cropping/#findComment-512448 Share on other sites More sharing options...
rofl90 Posted April 8, 2008 Author Share Posted April 8, 2008 allow_url_fopen On On Link to comment https://forums.phpfreaks.com/topic/100028-gd-cropping/#findComment-512452 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.