I am using following code to render a JPEG file, which is on disk, through the imagejpeg function:
//.... some headers....
$image = imagecreatefromjpeg($the_file);
imagejpeg($image);
Now, problem is, imagejpeg compresses the image (reduces the quality in my case) and does not output the same file. I know I can control the quality through a parameter, but it seems that imagejpeg does jpeg encoding/decoding. I don't want that (seems a waste, as the file is already a jpeg file). I want a simple function that will just "RENDER" the image (which is what I thought imagejpeg did till now). Can someone tell what to use?
You could use the readfile() function, to read the file from the disk, and output its content -- without doing any manipulation / transformation on it.
Related
I have created images using PHP image GD library and have them stored on my server, For the purpose of keeping some images uncached I wrote php code to fetch some specific images and output the php page as image through php headers:
$image=imagecreatefrompng($image_location);
header("Content-Type: image/png");
imagepng($image);
imagedestroy($image);
surprisingly, the image from php page is always slightly smaller in size than the original static file using which the image was created. Original PNG file was 11.5KB in one case, while the php png file of the same static file was 11.3KB
The original png image was created using
imagecreate(), imagecolorallocate(), imagettftext()
and
imagepng($image,$location,9,PNG_ALL_FILTERS)
Why is the original image itself always bigger than the original? How can I reduce the size first time itself? Is there something "un-optimized" about my code?
Please help me out, even a 10% saving on size will help me hugely.
I have a doubt. Is there a problem if I force imagejpeg to save a .png file?
Like in this example:
imagejpeg($img,$_SERVER['DOCUMENT_ROOT']."/test.png",80);
I want to use this method because I can use the $quality filter.
In this method I have a saved .png file for about 25kb, but if I use imagepng my image is for about 200kb (I used the $quality level from 0-9 but I didn't saw any changes, only -20 kb).
I don't want to make a mistake because my website is generating .png images every second.
Method 2.
I tried to compress the .png images with pngquant but I have no idea how to do it when I am using imagepng function.
I tried something like this, but It doesn't work if I pass the image to the function.
ob_start();
imagepng($img);
$png = ob_get_clean();
file_put_contents($_SERVER['DOCUMENT_ROOT']."/test.png", compress($png);
function compresss($img)
{
//...
}
In other cases if I have $png = $_FILES['file']['tmp_name'] is working. So is there a way to compress with pngquant, or is there a problem if I force imagejpeg to save a .png file?
Your forcing just the filename of the created file. The resulting file will be jpg with all its properties. You will not get a compressed png file if you use the imagejpeg() method.
You need to understand the difference of the both formats. While jpg is a compressed format which loses image information when compressing, png is a lossless format. png also has a compression level, but since no image informatio is destroyed, it will be bigger as an jpg file.
If you use imagepng($image, $filename, 9) you get a png file with the best compression.
You should use imagepng() instead of imagejpeg() to create a png file.
http://php.net/manual/fr/function.imagepng.php
Title is pretty much self explanatory...
Here is my simple code :
$img=imagecreate(300,300);
$black=imagecolorallocate($img,0,0,0);
$white=imagecolorallocate($img,255,255,255);
imagefill($img,0,0,$white);
imagerectangle($img,10,10,289,289,$black);
header ('Content-Type: image/png');
imagepng($img);
Now... If you save the exported file and upload it to yahoo smushit
http://www.smushit.com/ysmush.it/
You may notice that it can be compressed even more.
My goal is to export a compressed png image directly from php (on the fly) and that it would be fully optimized (compressed).
I have notice that imagepng have a compression parameter 0-9 but for some reason it is not working for me...
I know we can use imagecreatefrompng and imagejpeg functions to convert PNG to JPEG. But it creates a new file.
I just need file_get_contents() to store the file into database. That means I need JPEG file contents from my PNG file
eg:-
imagejpeg(imagecreatefrompng($_REQUEST['sig_data']), "test.jpeg");
$appSig = addslashes(file_get_contents("test.jpeg"));
Above example I need to create a 'test.jpeg' file. If I didn't pass that as an argument to imagejpeg function, it print the file in screen. And I can't do object buffering also. imagejpg function just return the status.
Admittedly somewhat hacky, but you can capture the direct output of imagejpeg:
ob_start();
imagejpeg($resource); // no file name
$imageData = ob_get_clean();
So i have an iphone app that that uploads an image to my webserver and i looked around and people seem to be doing something like
$data = file_get_contents($_FILES['file']['tmp_name']);
$image = imagecreatefromstring($data);
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
I looked at the php docs, but i still don't understand what the header() does; does it convert the image into whatever format i want?
And for the imagepng(), where is the image outputted to? memory? is that why i need the imagedestroy()?
and where would i put in
move_uploaded_file()
Thanks in advance!
This code is intended to return as output an image - you could use it as a valid src for an image tag. That is, you could do this:
<img src="thatfile.php?something=1" />
The headers tell the browser that the data the server is going to send is an image (a PNG image, specifically).
In your example code, the file never actually gets written anywhere: the data stays in memory until the script ends, then it is simply "forgotten". imagedestroy frees up the memory and is good practice, but it really isn't necessary since the memory will be garbage collected after the request ends. If you want to preserve the image in a file, you'd have to use one of the related functions such as imagepng: http://www.php.net/manual/en/function.imagepng.php. The only difference between writing the file or not in your example code is the lack of a second argument for imagepng - second argument would be the desired file path.
It would help to read through the docs on this entire subject to gain a firm grasp of how these functions work and what each does. There are plenty of demos on the doc pages that show this in action.
This particular example gets the image uploaded through POST from the $_FILES array and simply outputs it back to the browser. The header is there to inform the browser that the content following is a PNG image.
Since you create an image from a string, it doesn't have "an extension". It's just an image resource at this point. You can create an actual file from it using imagepng, imagejpeg or any of the other methods to save an image resource to a file. You decide the extension (and file name) at that stage yourself.
E.g.:
imagepng($image, 'path/to/file.png');
and where would i put in move_uploaded_file()?
You wouldn't, since you don't have an uploaded file, only a string.
Header is purely for the server to let the browser know "Oh hey this is a png image please render it so"
imagepng encodes it into the png format and "prints" to the output
imagedestroy frees the memory taken by the image resource.
If you need to force extension you can use mod_rewrite
Here's a sample couple lines from my .htaccess:
RewriteEngine on
RewriteRule images/000000/00FF00/newmyinfo.jpg images/newmyinfo.php?bgcolor=000000&color=00ff00 [L]
Hope this helps!