I have written a script image.php that is quite long, but I’ve just copied a portion of the code below.
Note that:
$image = $_GET['ph'];
$w = $_GET['w'];
So that:
function getImage_w($image, $w){
$ext = strrchr($image, ".");
if (strtolower($ext) == '.jpg' || strtolower($ext) == '.jpeg'){
header('Content-type: image/jpeg');
$src_im_jpg = imagecreatefromjpeg($image);
$size = getimagesize($image);
$src_w = $size[0];
$src_h = $size[1];
$dst_w = $w;
$dst_h = round(($dst_w/$src_w)*$src_h);
$dst_im = imagecreatetruecolor($dst_w,$dst_h);
imagecopyresampled($dst_im,$src_im_jpg,0,0,0,0,$dst_w,$dst_h,$src_w,$src_h);
imagejpeg($dst_im);
}
}
This script takes a two GET variables, $_GET['ph'] and $_GET['w'], ph defines the URL of the image, and w defines the destination resize width.
For example: If my script is in a libs/ directory, and my photos are in a resources directory, and I want to display my photo with width = 100, I have to use this html:
<img src="libs/image.php?ph=../resources/myimage.jpg&w=100">
My problem is, my images appear in the browser correctly, but with a bad url format. I’d like to put images alt attribute instead of this url in the images property.
And to automatically apply the image.php for an image request.
Also, I have a problem when the user wants to save the image in his desktop, the image saves under a name image.php, which is not desirable.
Thank you in advance.
Note that you can’t use .. in the url, so instead use root-relative url. To do this you need to add the document_root the the path in you php file:
Users saving the image will get the normal image name, but with a ‘-‘ and the size added to the end.
Not sure what you want with the alt. Also I don’t think it is wise to rewrite all images, so this script only rewrites images the start with /resize/…
http://www.example.com/hosting.jpg is not possible, as the php doesn’t know in which folder to look for this image, and to what size to resize it.