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...
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 PNG file with quite decent quality and not at all small size. I convert it to PDF like this(in php):
$imagick = new Imagick();
$imagick->readImageBlob($image);
$imagick->setImageFormat('pdf');
echo $imagick->getImageBlob();
And I get a PDF file with my image in it. But the quality gets very poor. I thought I might fix with trying different density setting it with setResolution method. But this lead to either the image becoming very small or getting even worse quality. What am I doing wrong?
I have the following code in PHP to take the screenshot of first page of the PDF.
$name = getcwd()."\\testfile";
$img = new imagick();
$img->setResolution(200,200);
$img->readImage($name.'.pdf[0]');
$img->setImageResolution(100,100);
$img->resampleImage(100,100,imagick::FILTER_LANCZOS,1);
$img->setImageCompression(\Imagick::COMPRESSION_ZIP );
$img->setImageCompressionQuality('0');
$img->setImageFormat('png8');
$img->writeImage($name.".png");
header("Content-type : image/png");
echo $img;
This code produces the PNG of 62kb only in the Google Chrome's Resource monitor tab. But the image which is written by Imagick() is above 114kb. Just to make sure image isn't compressed and or any other issues i have used a online service called TinyPNG and they compressed the image shrinking it to exactly 62kb i get in browser...
What could be wrong in this code? Also i am using PNG8 format because thats more efficient.
Best
Ahsan
I think this is caused by your writeImage statement. If you write a PNG image without specifying png8: specifically in the filename your image will not be stored in that format. In essence setImageFormat will only affect when you retrieve the image as a string (echo $img).
If you do the following:
$img->writeImage ('png8:' . $name . ".png");
it should be stored as a png8. You can verify this with identify -verbose and checking the Depth / Channel Depth.
These are the default compression methods used for the following common image formats:
PNG: Imagick::COMPRESSION_ZIP
JPEG: Imagick::COMPRESSION_JPEG
GIF: Imagick::COMPRESSION_LZW
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!
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.