Reputation: 127
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);
Upvotes: 3
Views: 540
Reputation: 239461
What your code does is more or less as follows:
// Load the PNG file from disk into memory
$im2 = imagecreatefrompng($image)
$im2
is now a resource
, 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.
// Some code which works with the image in memory, adding your watermark?
imagecopy() and more code here
// Tell the browser that we're output a JPG
header("Content-Type: image/jpeg");
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 echo
or just by having text/whitespace outside of <?php ?>
tags.
// compress as a jpeg, and send to browser
imagejpeg($im2,'',50);
imagejpeg
takes 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 be null
, not ''
. The final parameter, 50, specifies the jpeg quality as percentage. 100 is high-quality, 0 is low quality.
Upvotes: 3
Reputation: 44078
The imagecreatefrompng
is loading the image data from a PNG file. The image data at that point is just GD-specific pixel data.
The imagejpeg
outputs the pixel data as a jpeg. So your header should be specifying jpeg in this case.
Upvotes: 1
Reputation: 33388
The code is loading an image into a GD resource from a PNG file, then manipulating it, and finally outputting the image in a different format (JPEG) to the browser.
When you call imagecreatefrompng
, you're not just specifying that GD should use that file as its current working copy of the image, you're telling it to read the file, decode it from the PNG format, and load it into memory in some unknown internal format. At this point, the format of the file that the image was originally read from no longer matters. When you output it, you have to specify the format you want, since it no longer has a format associated with it. This is why you're using imagejpeg
.
Upvotes: 1
Reputation: 6331
The imagejpeg function outputs the image in JPEG format regardless of the format of the original object. If you want to preserve the original format you need to determine this when loading the file and then use a different function when producing output - There are functions imagepng, imagegif, etc.
Upvotes: 1