1
header('Content-type: image/jpeg');

$url = $_GET['url']; 

$ch = curl_init(); 

curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); 

$file = curl_exec($ch); 

curl_close($ch);

echo $file;

Can anyone please tell me how I can change the width of $file to 300px?

1 Answers1

1

If the image is heading for a browser inside an <img> tag, then you can get the browser to rescale it by changing the width attribute.

Otherwise you need to grab the file and rescale it in your PHP code before outputting it.

You can use imagecopyresampled() to do that, calculating the height. Then outputting the result.

Example, following on from obtaining the image in to $file from the code above.

$img = @imagecreatefromstring($file);

if ($img === false) {
    echo $file;
    exit;
}

$width = imagesx($img);
$height = imagesy($img);

$newWidth = 300;

$ratio = $newWidth / $width;
$newHeight = $ratio * $height;


$resized = @imagecreatetruecolor($newWidth, $newHeight);

if ($resized === false) {
    echo $file;
    exit;
}

$quality = 90;

if (@imagecopyresampled($resized, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height)) {
    @imagejpeg($resized, NULL, $quality);
} else {
    echo $file;
}
Orbling
  • 20,413
  • 3
  • 53
  • 64
  • hey, thanks for the reply! i tried list($width, $height) = getimagesize($file); echo $width; but getimagesize doesn't work for some reason, and i don't know how else i would get the original size. –  Feb 21 '11 at 17:53
  • @krysis: In the event of an error, it'll try and send the original file, so if you are using it within an `` tag, best to set the width on it, you should anyway. Beware of running out of memory on the server if the filesize is big. – Orbling Feb 21 '11 at 18:15
  • will do! and thanks for the help again! really appreciate it :) –  Feb 21 '11 at 18:21