I am using the following code to dynamically generate a background image:
$image = imagecreatefromjpeg( 'FILENAME' );
header( 'Content-Type: image/jpeg' );
imagejpeg( $image );
Opera displays nothing when I try to view this script. Chrome and IE work fine, and if I add header( 'Content-Disposition: attachment; filename=download.jpg' );
Opera will download the image as expected (and the content is correct).
Is there a workaround for this, or some header that I am not setting? I am surprised that Chrome handles the image correctly, while Opera (Chromium-based) does not.
Update: the same problem is true if I use, e.g., Content-Type: image/png
and imagepng()
instead: loads in all browsers but Opera, and Opera downloads the file correctly.
Update': this is an extension problem, but I don't know why. Adding my site to the whitelist of uBlock makes everything work. My site has practically zero traffic and certainly doesn't spam anything, so this is odd.
In that case the cleaner solution would be just to put 'banner.php' in one of your images folder and put this in the .htaccess of the folder 'banner.php' is in:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}banner.php [NC,L]
(provided you have mod_rewrite loaded in Apache)
It checks if an image exists, if not it lets banner.php handle the job.
That way you can just use "banner.jpg" as the source and neither Opera or anyone else will know it's dynamically generated..
Funny - I'm doing the exact same and it opens in Opera just fine. Only difference is that I'm adding two lines:
header('Content-Type: image/jpeg');
imagejpeg($img);
imagedestroy($img);
exit;
What could help too is adding a "Content-Length" header with the result of getimagesize($img). Perhaps Opera assumes there's more to come - with a hard "exit;" i.c.w. "Content-Length" you can rule that out and hopefully resolve your issue.
Update: getimagesize() doesn't work on a dynamically generated image, but this one does:
header('Content-Type: image/jpeg');
ob_start();
imagejpeg($img);
$size = ob_get_length();
header("Content-Length: " . $size);
ob_end_flush();
imagedestroy($img);
exit;
Still not sure if it will resolve the Opera issue - but at least it's sending the correct headers..
The problem is outside the scope of PHP. I have μBlock installed in Opera, and the script generating the images is named banner.php
; EasyList (included in μBlock) by default blocks elements with src="banner.php"
. I guess it's time to rename it site-logo.php
.