Forum Moderators: coopster
I would like to create a resized version of said image, and either save it somewhere or simply display it on the page.
I have been having a look at some code but have not been getting anywhere with trying to piece something together.
I have used getimagesize() successfully to get info about the image and have tried using imagecopyresized() to display a resized copy, but with no luck.
Here is some sample code I was trying to adapt
// File and new size
$filename = '../path/to/my/file.jpg;
$percent = 0.5;
// Content type
header('Content-type: image/jpeg');
// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;
// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);
// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// Output
imagejpeg($thumb);
I think part of the problem is the
header('Content-type: image/jpeg'); bit, if I remove it I get a load of weird code on the page, and if I leave it in I just get a page which echoes its own url. Help!
<?
// Image file
$filename = "path/to/file.jpg";
// Set width and height you want
$desiredWidth = ###px;
$desiredHeight = ###px;
// Image details
list ($width, $height) - getimagesize($filename);
// Resizes height/width proportionally to desired width
if ($width > $desiredWidth){
$height = ($height/$width)*$desiredWidth;
$width = $desiredWidth;
}
// Resizes height/width proportionally to desired width
if ($height > $desiredHeight){
$width = ($width/$height)*$desiredHeight;
$height = $desiredHeight;
}
// Display image on HTML page
echo "<img height='$height' width='$width' src='$filename' />";
?>
or can you do this in a main script and resize with the HTML <img> tag?
This is very different to the OPs original query of using the GD library to actually create a new resized image and send this to the client. To be honest, using PHP to create a new image of the correct size is by far the better solution (over letting HTML do the resizing), particularly if this is then saved/cached server side.
$percent = 0.5;
With a linear scale factor of 0.5 you are potentially looking at a resized image which is 1/4 of its original size. To send the original image and let HTML do the work is an unnecessary bandwidth hog and the quality will be poor (in most browsers).
Using PHP to do the work will be quicker, better quality and will use less bandwidth - everyone's a winner.
To be honest, using PHP to create a new image of the correct size is by far the better solution (over letting HTML do the resizing), particularly if this is then saved/cached server side.
Interesting. How about when calling an image from a non-local site, would it still be better to resize/recreate with php?
Thanks again for your help, it was nearly there, just needed to save the file rather than output it, and I swapped imagecopyresized for imagecopyresampled, which produces a much better looking image:
<?php
// File and new size
$filename = 'pathtofile/testpic.jpg';
//echo($filename);
// Get new sizes
list($width, $height) = getimagesize($filename);
//figure out height ratios
$newwidth = 200;
$newheight = ($newwidth/$width) * $height;
// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);
// Resize
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// Save the image as 'mynewpic.jpg'
imagejpeg($thumb, 'pathtofile/mynewpic.jpg',85);
// Free up memory
imagedestroy($thumb);
?>