Problem with image_crop with negative value

See all posts Reply

Problem with image_crop with negative value new!
by WoofWoof75, 16 years, 7 months ago
Salut! Thank you very much for the wonderful Upload Class!

I'm currently using Upload Class 0.25 RC1 for multiple files uploading, but having some problems.

Each file uploaded will first be sent to create thumbnail, then again to resize uploaded image larger than 640 px to 640 px.

Sample #1: with $handle->image_crop = '0 0 -12px 0';
thumb1.jpg 1.jpg

You can see file uploaded in Sample #1 the larger image has a problem with the pure black part being converted into white. But Sample #2 with that image_crop turned off will not have the problem. I'm using image_crop to add a black space for me to add in watermark.png.

Sample #2: without $handle->image_crop = '0 0 -12px 0';
thumb2.jpg 2.jpg

After the uploading process completed, a thumbnails page of images users has just uploaded will be shown to user, but the thumbnail file doesn't exist yet... I'm thinking of adding a sleep() function waiting for thumbnails ready, or do you have a better solution?

I've tested this on both local and live servers, both giving the same problems.
Local: Apache/1.3.33 (Darwin) PHP/5.2.2
Live: Apache/1.3.37 (Unix) PHP Version 5.2.3

Merci beaucoup!

WoofWoof75Reply
Re: Problem with image_crop with negative value new!
by colin, 16 years, 7 months ago
It is a bug, thank you for reporting it.

The error occurs because when the class crops, with negative margins, it creates up extra space, which by default is black. PHP image_file() function fills that area, but spills on the picture itself if it contains a contiguous black area.

To solve it, change the following into:
// fill in from the edge of the negative margin
if ($ct < 0) imagefill($tmp, round($this->image_dst_x / 2), 1, $fill);
if ($cr < 0) imagefill($tmp, $this->image_dst_x - 1, round($this->image_dst_y / 2), $fill);
if ($cb < 0) imagefill($tmp, round($this->image_dst_x / 2), $this->image_dst_y - 1, $fill);
if ($cl < 0) imagefill($tmp, 1, round($this->image_dst_y / 2), $fill);
to
// fills eventual negative margins
if ($ct < 0) imagefilledrectangle($tmp, 0, 0, $this->image_dst_x, -$ct, $fill);
if ($cr < 0) imagefilledrectangle($tmp, $this->image_dst_x + $cr, 0, $this->image_dst_x, $this->image_dst_y, $fill);
if ($cb < 0) imagefilledrectangle($tmp, 0, $this->image_dst_y + $cb, $this->image_dst_x, $this->image_dst_y, $fill);
if ($cl < 0) imagefilledrectangle($tmp, 0, 0, -$cl, $this->image_dst_y, $fill);
This will be added in the next release.

As for the images not being created quickly enough, it is strange. When the class dumps the image, it does so, then only the script continues. So when the page with the link to the image is sent to the browser, the image has been indeed created. Maybe your live server uses a filer, and the files need a little bit of time to synchronise.

(edit: implemented a better fix)Reply
Re: Problem with image_crop with negative value new!
by WoofWoof75.com, 16 years, 7 months ago
Allo Colin,

Thank you for the fix.

By the way, regarding the "thumbnails" not showing... it occurs to both local testing server and the live server.

Maybe will just use the sleep() function then.

Merci beaucoup pour le fabuleux upload script! :-)

WoofWoofReply