1

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!

2
  • Define remote files... how will use access them? through HTTP, FTP etc? Commented Dec 12, 2011 at 11:01
  • i've edited it now, its just via the http URL. Commented Dec 12, 2011 at 12:10

2 Answers 2

1
<?php
    $remote_file = 'http://l.yimg.com/g/images/soup_hero-04.jpg';
    $new_width = 100;
    $new_height = 100;
    list($width, $height) = getimagesize($remote_file);
    $image_p = imagecreatetruecolor($new_width, $new_height);
    $image = imagecreatefromjpeg($remote_file);        
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    header('Content-Type: image/jpeg'); 
    imagejpeg($image_p, NULL, 100);
    imagedestroy($image_p);
?>

NOTE: ensure no whitespaces before or after the php tags

Sign up to request clarification or add additional context in comments.

2 Comments

I get: "[function.getimagesize]: failed to open stream: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond" when using this
@user1093553: you may need to enable allow_url_fopen in your php.ini.
0

Huh, it seems that imagecreatefromjpeg accepts as a parameter url, take a look http://www.php.net/manual/en/function.imagecreatefromjpeg.php

imagecreatefromjpeg — Create a new image from file or URL

getimagesize works with URL too, suport was added in php 4.0.5 http://www.php.net/manual/en/function.getimagesize.php

4.0.5    URL support was added.

1 Comment

the imagecreatfromjpeg mentions that fopen wrappers need to be enabled. Is it likely that they aren't?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.