Jump to content

GD Cropping


rofl90

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.