I am writing a PHP script to dynamically resize an image. The image ID (from a MySQL db) is passed in like this : “http://localhost/getimage/1.htm”.
When I go directly to the scrippt with a URL like above, the image is spat out perfectly. But for some reason (only in Chrome), when I link that URL to an tag it starts acting weird. When the page first loads the image loads fine, but then the loading bar spins for about 5 seconds and suddenly the image disappears and Chrome shows a “Failed to load resource” error.
Does anyone have any idea what might be causing this and, if so, how to stop it? I thought that it might have had something to do with AdBlocker, but I have disabled that and it is stil happening.
Cheers.
Edit: This is the code that I am using:
header('Content-Type:'.$file['type']);
header('Content-Length: ' . $file['bytes']);
// Get size of original image
list($o_width, $o_height) = getimagesize($file['src']);
// Default width and height
if (is_null($width)) {
$width = $o_width;
}
if (is_null($height)) {
$height = $width;
}
// Create image frame
$image_p = imagecreatetruecolor($width, $height);
// Generate image depending on source type
switch ($file['type']) {
case "image/jpeg":
default:
$image = imagecreatefromjpeg($file['src']);
break;
case "image/gif":
$image = imagecreatefromgif($file['src']);
break;
case "image/png":
$image = imagecreatefrompng($file['src']);
break;
}
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $o_width, $o_height);
// Output
imagejpeg($image_p, null, 100);
exit;
As you can see I am setting the content types. If it was the htm extension, confusing it, would that explain why the image loads correctly, and then unloads itself?
this is not Chrome only, but IMHO it is wrong that you have this at the beginning: header(‘Content-Length: ‘ . $file[‘bytes’]); because you don’t know the actual file size, since you create the image on the fly with imagejpeg($image_p, null, 100); Try commenting out the header(‘Content-Length… line, it might fix it.