-3

I want to download this image: http://imgs.xkcd.com/clickdrag/1n2w.png

But the image is too large for me so i want to resize it to lets say 100 times smaller than it is now. Also i want the image to have its original name (in this case 1n2w.png).

For downloading i was thinking of using somet
$content = file_get_contents('http://imgs.xkcd.com/clickdrag/1n2w.png');
file_put_contents('/images', $content);

But it didnt work. Maybe i need to use curl for this? As for the resizing part i dont know what to use, so if possible i would like too see some suggestions on this.

Edgar
  • 1,113
  • 3
  • 16
  • 30
  • 1
    file_get_contents some does not work for some security reason, you can use curl. For image re-sizing, first save file your location, then re-size it. – Dieepak Mar 28 '13 at 07:34
  • @Neo, is it only possible to download first and then resize? Because it takes really long time to download this large image (like a couple of mins, and i have 225 of these images, so i was wondering if i could speed up the process by resizing it first before downloading) – Edgar Mar 28 '13 at 07:35
  • 2
    yes you have to use the curl for downloading the image. It is more efficient than the file_put_contents and without downloading you can not resize it. So download it and then resize it by using php-resize scripts and then remove the original large size image – Kalaiyarasan Mar 28 '13 at 07:36

1 Answers1

0

For image re-sizing you can use it.

    $percent = 0.5;

    $filename = '/home/Pictures/downloaded_file.jpg';

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

    list($width, $height) = getimagesize($filename);

    $new_width = $width * $percent;

    $new_height = $height * $percent;

    $image_p = imagecreatetruecolor($new_width, $new_height);

    $image = imagecreatefromjpeg($filename)
    ;
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

    imagejpeg($image_p, null, 100);
Dieepak
  • 546
  • 2
  • 7
  • This works but only for jpeg images, can this be used for png images or do i need to convert my images to jpeg? – Edgar Mar 28 '13 at 08:41
  • Slightly changes here need to make, you need to change in header then after it you need use imagepng instead of imagejpeg function and use imagecreatefrompng instead of imagecreatefromjpeg. – Dieepak Mar 28 '13 at 08:44
  • Changed all 3 things (`image/png`, `imagecreatefrompng`, `imagepng`) but it doesnt show anything now – Edgar Mar 28 '13 at 08:52
  • Please look into this : http://stackoverflow.com/questions/5064839/png-image-resize – Dieepak Mar 28 '13 at 09:01