I've edited the basic php.net script for resizing to work for my specs. It works with local files but when it comes to remote files (this is trying to resize images from a wordpress blog to go with the blog posts) I can't seem to figure out how to GRAB the image, manipulate it, then output it.
// The file
$filename = 'like-father-like-son.jpg';
// Set a maximum height and width
$width = 150;
$height = 150;
// Content type
header('Content-Type: image/jpeg');
list($width_orig, $height_orig) = getimagesize($filename);
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// Output
imagejpeg($image_p, null, 100);
The above script works, but I want $filename to access: http://USERNAME.files.wordpress.com/DATE/like-father-like-son.jpg .
EDIT: This script works fine. It was a problem with the network I was trying to run WAMP locally on. That'll teach me to try doing it during lunchtime at work! Apologies for wasting anyone's time and THANK YOU to those who contributed.
Please feel free to use the script below, it resizes an image from file or URL so that it fits within the dimensions declared (as long as its a jpeg) and keeps the same aspect ratio.
EDIT 2: I thought I'd just add this here since I had this trouble implementing it. I personally had two further problems, I wanted to use this to produce an image AS WELL as printing some text which with the 'header('Content-Type: image/jpeg'); doesn't work. I also wanted the resize script to run with more than one image.
Solution: Place the image resize script in a separate file (imageresizer.php) and use GET data to send the image's URL to it. So the top part of the image resize script would now read:
// The file
$filename = $_GET['imagesrc'];
and at the bottom I had the image return:
//Output
return (imagejpeg($image_p,null,100));
The script to reference and render the images (the one sending the data to image resizer) would have something resembling this:
$imagesrc = "put your image sourcing script here";
echo '<img src="imageresizer.php?' . $imagesrc . '" />';
I hope this helps someone out there!
remote files... how will use access them? through HTTP, FTP etc?