3

I use this script to download and resize a remote image. In the resize part something goes wrong. What is it?

<?php
$img[]='http://i.indiafm.com/stills/celebrities/sada/thumb1.jpg';
$img[]='http://i.indiafm.com/stills/celebrities/sada/thumb5.jpg';
foreach($img as $i){
    save_image($i);
    if(getimagesize(basename($i))){
        echo '<h3 style="color: green;">Image ' . basename($i) . ' Downloaded OK</h3>';
    }else{
        echo '<h3 style="color: red;">Image ' . basename($i) . ' Download Failed</h3>';
    }
}

function save_image($img,$fullpath='basename'){
    if($fullpath=='basename'){
        $fullpath = basename($img);
    }
    $ch = curl_init ($img);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
    $rawdata=curl_exec($ch);
    curl_close ($ch);




    // now you make an image out of it

    $im = imagecreatefromstring($rawdata);

    $x=300;
    $y=250;

    // then you create a second image, with the desired size
    // $x and $y are the desired dimensions
    $im2 = imagecreatetruecolor($x,$y);


    imagecopyresized($im2,$im,0,0,0,0,$x,$y,imagesx($im),imagesy($im));


    imagecopyresampled($im2,$im,0,0,0,0,$x,$y,imagesx($im),imagesy($im));

    // delete the original image to save resources
    imagedestroy($im);



    if(file_exists($fullpath)){
        unlink($fullpath);
    }
    $fp = fopen($fullpath,'x');
    fwrite($fp, $im2);
    fclose($fp);

    // remember to free resources
imagedestroy($im2);



}
?>

1 Answer 1

2

When I run it, PHP gives me the following error:

Warning: fwrite() expects parameter 2 to be string, resource given ... on line 53

fwrite() writes a string to a file. You want to use the GD function imagejpeg() to save the GD resource to a file. It works for me when I change

$fp = fopen($fullpath,'x');
fwrite($fp, $im2);
fclose($fp);

to

imagejpeg($im2, $fullpath);

On an unrelated note, if all you're doing with cURL is grabbing the files, you can simply use file_get_contents() instead of cURL, assuming PHP is configured to allow full URLs in fopen functions. (I believe it is by default.) See the Notes section on the file_get_contents() manual page for more info. The function is binary-safe, so it works with images in addition to text files. To use it, I just replaced all six lines of the cURL functions with this line:

$rawdata = file_get_contents($img);

Update:
In response to your question below, you can specify a new file name for them in the array keys like so:

<?php
$img['img1.jpg']='http://i.indiafm.com/stills/celebrities/sada/thumb1.jpg';
$img['img2.jpg']='http://i.indiafm.com/stills/celebrities/sada/thumb5.jpg';
foreach($img as $newname => $i){
    save_image($i, $newname);
    if(getimagesize(basename($newname))){
Sign up to request clarification or add additional context in comments.

2 Comments

Would it be possible to define a custom name for the image in: $img[]='i.indiafm.com/stills/celebrities/sada/thumb1.jpg'; ad pass it some way into the function?
@mark Sure. Code added in answer above, since there's not enough room here.

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.