Show one image in different qualities using PHP - php

Is there a way to show one image in different qualities on fly? ie:
<img src=show_img.php?img_src=http://www.simpsoncrazy.com/content/pictures/homer/HomerSimpson3.gif&img_quality=25>
<img src=show_img.php?img_src=http://www.simpsoncrazy.com/content/pictures/homer/HomerSimpson3.gif&img_quality=50>
<img src=show_img.php?img_src=http://www.simpsoncrazy.com/content/pictures/homer/HomerSimpson3.gif&img_quality=75>

First, you can use a tool like Imagick to do the compression, resizing, etc.
Second, while you have the right idea in pointing your img src to a PHP script, consider the security implications of allowing a full URL as a parameter in the request. What if someone uses your code to point to an image on a different website with copyrighted material? Don't leave yourself exposed as an open proxy...

The gif format doesn't have the ability to be output with different quality settings. When you output a gif using GD, it automatically uses the GIF87a format, which supports up to 8 bits per pixel, unless you use imagecolortransparent, which will then set the format to GIF89a.
However, .jpg does and can be set in the 3rd (optional) parameter of imagejpeg (see how to load a .gif from a URL here)

Related

Clean solution to set DPI on created PNG images

I would like to create PNG images using PHP on a website. These shall be printed at a defined scale. So I would like to set the DPI value of the images using PHP directly. Unfortunately I did not find any function call for this.
Is there any function that can set/update metadata of PNG files?
Maybe an other solution is more reasonable as using a HTML-Wrapper with CSS style sheet for printing which externally defines the resolution. But I would prefer the "directly on the image" approach...
PNGs can contain arbitrary headers. If you look at the PNG specification, you can add tEXt blocks (which are called chunks) to a given PNG. See section 4.2.3 of the specification for more information on tEXT chunks.
As an example, Adobe Photoshop adds meta XML to its PNGs. I'm not sure if GD supports this, but I'd look there to start. It's definitely possible.
Here is some PHP code that deals with parsing PNG chunks. It might steer you in the right direction. http://code.svn.wordpress.org/imagelibs/libpng.php
Here's an screenshot for a text editor of a PNG, showing the XML that was generated by Photoshop. https://stackoverflow.com/a/14356339/278976
THe pHYs chunk (Physical resolution) lets you set a DPI (well, actually pixels by meter, but it's just a unit conversion). Of course, the PNG reader might ignore it.
PHP does not include (AFAIK) support for reading/writing full PNG metadata, you must do it yourself, see eg
The easiest way is to use ImageMagick, as suggested in this answer. If You want to set PNG resolution in pure PHP, you may look at my answer to the similar question.

Trouble with Interchange.js in Zurb Foundation when images are being dynamically resized by PHP class

I'm building a responsive site using Zurb Foundation.
I have a PHP script which will resize and caches an image using gdlib if you append a query string with new dimensions in the URL. For example to resize an image to 300px wide:
http://www.mydomain.com/images.php?imgfile=path/to/picture1.jpg&w=300
I am also using some HTACCESS rewrite rules to make this URL pretty and avoid having a query string. So this URL gives the same result as above:
http://www.mydomain.com/img/300w/path/to/picture1.jpg
The PHP file performs some simple arithmetic to constrain by width or height, checks if the resized version is already in cache, if so outputs it, if not, resizes the images, saves it using imagejpeg and outputs it with header("Content-type: image/jpeg");
I am also using Zurb Foundation and want to use the interchange javascript like so:
<img src="http://www.mydomain.com/img/300w/path/to/picture1.jpg"
data-interchange="[http://www.mydomain.com/img/300w/path/to/picture1.jpg, (default)],
[http://www.mydomain.com/path/to/picture1.jpg, (medium)]">
However, this does not seem to work. Only the 300px is shown for both breakpoints. After much testing it's clear that only what's in the src attribute is taking. The images passing through the resize script don't work. This is true even if it should be using the medium image which is the direct path the full size image.
I tried to debug the interchange javascript, but am not that skilled in Javascript.
Any help or advice would be appreciated. Someone must be trying to using dynamically resized images with PHP using interchange.js on a responsive site.
There is no need for debugging interchange, it works pretty well.
First, have you included the foudation.js file before interchange.js (dependancy) ?
Tip for debugging: try with default/medium/small and use different images (ex: different color rectangles) to quickly notice changes.
Also, in your example, there is only one path (see below) and you're having a "default" named-query. What is the point of loading the same image twice ? You might want your default size to be in src="", and your (typically) bigger sizes thereafter ?
What interchange does is letting the src"(ex: small.jpg)" loads as usual (hence it's displayed without js enabled) and THEN loads a bigger image depending on the named-query/media-query. So perhaps you could generate all your image size on upload (with no check for size existance needed). At least, it's the way I do it with wordpress.
<img src="http://www.mydomain.com/img/default-size/300w/path/to/picture1-small.jpg"
data-interchange="[http://www.mydomain.com/img/medium-size/800w/path/to/picture1-medium-sized.jpg, (medium)],
[http://www.mydomain.com/img/large-size/1200w/path/to/picture1-large-sized.jpg, (large)]">
As I can see on the Zurb Foundation Github repo Issues there may be a problem with url containing parameters and their regular expression

reduce jpeg filesize "on the fly" with php

I would like to lessen the image size (and quality) of a jpeg on the fly when a user is using a certain browser, OS or screen size (used to distinguish mobile from desktop users). How do I do this?
I imagine hat i'd call a script that would return the image e.g.
<img src="<?resize.php?file=test.jpg&quality=75?>"
and in my resize.php I would need to convert test.jpg to be the $image ressource for imagejpeg () with which I can set the image quality.
How do I go about the conversion? also will
echo imagejpeg($img,NULL,$quality);
result in displaying an image using above html snippet?
You would need to write the resizing routine, and then have it saved someone web accessable. And I would cache the file the first time you resize it so you don't have to resize it every time someone loads a page.
Two objections:
img src requires an URL, not an image itself (actually, can be a data URL, but that's so wrong especially when trying to compress)
why rerender images for each hit? That would be horribly slow and inefficient. Just prerender them and select one of the variants according to user's specs.
If you still want to resize on the fly, then have a script resize.php or similar that will output the image of the given size. The script has to set the correct Content-Type header (image/jpg, for your example). Then use this as a tag:
<img src="resize.php?file=test.jpg&quality=75">
(note: no PHP tags, this is just a link!)

javascript/jquery: get info for a remote image

I have an image that has "src" pointing to an external server, like this:
<img src="http://somewhere.com/script.php?id=1234">
The image returned is .png and it can be returned as a "X" image or "O" image.
How can I determinate if the image is X or O with javascript/jquery? Calculating md5 of the loaded image? In case how can I access to the image bytes and calculate md5?
First of all, what you're doing is probably very inefficient. Because you load the image from a dynamic PHP script, most browsers will not cache it. Furthermore, loading images from another site you do not have access to is always considered bad practice.
Anyways, the easiest way to do this is using a server side language like PHP. Then you can get the file size using either curl, fsocketopen, get_headers or fopen. By comparing this number to the known file size of the images, you know which image is loaded. Take a look at this page for an example using curl.
Alternatively, you can also do this using JavaScript (if you really must): take a look at this stackoverflow question.
You could draw the image into a canvas object and then check the pixels. E.g. an X would perhaps have a black pixel at the top-left corner while an O would have a transparent or white one (depends on the images of course).

How to resize linked images dynamically in PHP?

On my site I have given an option to user to choose thier profile image
Type link of an image
Image is a url link, and first I want it to resize to 400x300 (image's original size doesn't matter), and then display it on my web page.
Something like below:
<img src="http://mywebsite.com/resize.php?image=http://someotherurl.com/upload/image2.jpg&width=400&height=300" />
anyone knows this kind of script, please tell me how to solve this issue.
Thanks
A recent post:
https://stackoverflow.com/questions/1302464/php-image-resize-my-upload-script
has some code and comments that may give you some pointers. Otherwise may I suggest
http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php.
Good luck!
If you have the GD extenstion, you can use imagecopyresampled (the documentation also features some examples). However, if the image to be resized is large and there is a low memory limit on your server, you may run out of memory.
I don't have ready to use source code, but it should look like:
Load image pointed by image parameter into object of ImageMagick (or other graphics library).
Resize it.
Send content to output stream.
Optionally you could:
Check if loaded file is image (plus other validation checks).
Save resized image on disk and serve it from disk next time (if you do it often).
Check docs of you favorite graphics library used in PHP for details.
Good luck!
Use the Class called - class.upload.php.
Find it at: PHP Classes
We use it at all times in many of our work.
The name is deceptive but actually it is an uploader as well as image processor. It has a very big list of functionality for resizing images, adding text to images, converting formats, etc. etc.
There is sample code which shows how to read an Image from server, modify it and finally send it directly to browser without having to create a temp file on server.
HTH

Categories