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.
echo var_dump($variables);?$row['original']?