Question

Is there a way with to display an image on a server reduced and resized without saving an reduced one? Like if an image was 500x500 250KB, I want it to display 50x50 45kb in a user's browser.

If there isn't a way, how can I temporarily create one to go to a folder and delete later after sometime has pasted?

Was it helpful?

Solution

header("Content-type: image/jpeg");
$image = imagecreatefromjpeg($url);
$thumbImage = imagecreatetruecolor(50, 50);
imagecopyresized($thumbImage, $image, 0, 0, 0, 0, 50, 50, 500, 500);
imagejpeg($thumbImage,"",85);
imagedestroy($image);
imagedestroy($thumbImage);

But you should cache the result somehow to lower the load on the server.

OTHER TIPS

I would strongly suggest that rather than trying to resize down a large image that you at upload time store a smaller version and keep the original version. Using cloud hosting like S3 you can do this cheaply.

The most logical process is to perform the expensive operation (scaling the photo) once rather than multiple times.

In answer to your other question you can use GD or imagemagick to scale down an image. There are hundreds of tutorials on how to do this. If you want to go down this route I would Google a tutorial and have a go.

It sounds like all you want to do is set the height and width attributes of the HTML image tag.

<img src='blah' height=50 width=50 />

percentages can be used also.

CSS allows for a little more control, and can be applied to all images (or all images of a certain class). With it you can specifcy max-height and max-width which will not effect smaller images. Below will apply to all images with class resizedimages

.resizedimages {max-height: 400px; max-width: 400px}

If you are not interested in saving a resized image, then this sounds like entirely a client side issue. PHP has nothing to do with it (except perhaps rendering the necessary HTML)

NOTE: If the user goes to right click and save on the image it will still be the full 250kB. These are purely display issues. If this aspect is important to you then I don't know any way other than saving a reduced copy.

These probably arent the best resources, just a few that came up near the top of google.

http://www.w3schools.com/tags/tag_img.asp
http://www.fastechws.com/tricks/web/image-max-width-height.php

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top