I’m looking at this code and have been through the docs but still don’t understand how this should work. The code works fine just as it sits but I’m wondering if I should be outputting the header as png instead of jpeg.
What exactly is going on in this code? Is the png image converted to jpeg?
What I ultimately want to do is to watermark all gif, jpg, bmp and png images in a single directory. I’m outputting all headers, regardless of the image type as jpg. Is this correct? I hope I’m making sense here, I’m kind of tired.
$im2 = imagecreatefrompng($image)
imagecopy() and more code here
header("Content-Type: image/jpeg");
imagejpeg($im2,'',50);
What your code does is more or less as follows:
$im2is now aresource, referencing a image. Once in memory, it is not a png or a jpeg; it is a raw, uncompressed data. The “format” of an image specifies how that raw data is packaged and formatted; at this point, it has no such formatting. It’s just data in memory.If you request a jpg (ie http://host.com/image.jpg) then the server takes care of writing this header for you. If you’re making a JPG on the fly via PHP you have to manually output the header. Otherwise, PHP assumes you’re writing HTML and outputs the appropriate headers for you as soon as write anything to stdout, either via
echoor just by having text/whitespace outside of<?php ?>tags.imagejpegtakes the raw image, compresses it as a jpg, and writes it to either a file (if you give it a filename) or stdout (which sends it to the browser). Technically to output to the browser, the 2nd argument should benull, not''. The final parameter, 50, specifies the jpeg quality as percentage. 100 is high-quality, 0 is low quality.