4

I've got a script that saves user-uploaded images to a MySQL database, and then on another location they can access them. There is also an option where they can crop the image, which works correctly to. However, when I try to render a cropped image (normal images work fine), I get the following:

Warning: imagecreatefromstring(): gd-png: fatal libpng error: Read Error: truncated data in /home/retweety/public_html/uploadr/get.php on line 92
Warning: imagecreatefromstring(): gd-png error: setjmp returns error condition in /home/retweety/public_html/uploadr/get.php on line 92
Warning: imagecreatefromstring(): Passed data is not in 'PNG' format in /home/retweety/public_html/uploadr/get.php on line 92
Warning: imagecreatefromstring(): Couldn't create GD Image Stream out of Data in /home/retweety/public_html/uploadr/get.php on line 92
Warning: imagecrop() expects parameter 1 to be resource, boolean given in /home/retweety/public_html/uploadr/get.php on line 93
Warning: imagepng() expects parameter 1 to be resource, null given in /home/retweety/public_html/uploadr/get.php on line 96
Warning: imagedestroy() expects parameter 1 to be resource, null given in /home/retweety/public_html/uploadr/get.php on line 97

This is my code:

$imgdata = $row['original']; // Line 86
$variables = json_decode($row['cropped']); // Line 87

$im = imagecreatefromstring($imgdata); // Line 89
$im2 = imagecrop($im, ['x' => $variables->x, 'y' => $variables->y, 'width' => $variables->width, 'height' => $variables->height]); // Line 90
if ($im2 !== FALSE) { // Line 91
    header('Content-Type: image/png'); // Line 92
    imagepng($im2); // Line 93
    imagedestroy($im2); // Line 94
} else { // Line 95
    echo "Could not crop image."; // Line 96
} // Line 97

Edit: This is a var_dump of $variables for one of the pictures

object(stdClass)#3 (4) { ["x"]=> int(87) ["y"]=> int(15) ["width"]=> int(142) ["height"]=> int(82) }

$imgdata contains raw image data. I have the base64 encoded version of the data for the same image on the URL below. I didn't paste it here because it's too long.

https://uploadr.retweety.deals/katherine/1041f1a2511aa8179d9809dad66418ead85a0790/75cc53e4ee0f96acd2a2876c2484f91fcfc6c43d/normal

2
  • can you show the output of echo var_dump($variables);? Commented Feb 11, 2018 at 10:17
  • Could you also add an example value for $row['original']? Commented Feb 11, 2018 at 10:40

1 Answer 1

1

I tested your code with the $imgdata. for me it looks like the image source stored in the database is corrupt. Otherwise you would be able to display the original image without error. I wasn't able to display the image by this code. I got the same errors.

$imgdata = base64_decode($imgdata);
$im      = imagecreatefromstring($imgdata);
if ($im !== false) {
  header('Content-Type: image/png');
  imagepng($im);
  imagedestroy($im);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.