Skip to content
WJunction - Webmaster Forum

remote imge resize

Status
Not open for further replies.
heyya,

im tryna resize a remote image with no luck, heres my code,

PHP:
        $pixels = getimagesize($url); // Get image size in pixels for remote.
        $width2 = $pixels[0];
        $height2 = $pixels[1];

        //Make a 90x90px thumbnail......
        $fileURL = ($uploadDir . '/' . $newname);
        //echo $fileURL;
        
        if ($exten == "image/pjpeg" || $exten == "image/jpeg") { $newThumbImg = imagecreatefromjpeg($fileURL); }
        
        if ($exten == "image/x-png" || $exten == "image/png") { $newThumbImg = imagecreatefrompng($fileURL); }
        
        if ($exten == "image/gif") { $newThumbImg = imagecreatefromgif($fileURL); }
        
        $ratio = $width2 / $height2; // calculate the ratio

        if ($ratio > 1 ) {
        
            $newW = $thumbPixels;
            $newH = $thumbPixels / $ratio;
    } else {
            
            $newH = $thumbPixels;
            $newW = $thumbPixels * $ratio;
    }
    
    //function for resize image.

    if ( function_exists ( 'imagecreatetruecolor' ) ) {

            $newImg = imagecreatetruecolor($newW, $newH);
    } else {

            die("Error: Please make sure you have GD library ver 2+");
    }

    //the resizing is going on here!

    imagecopyresized($newImg, $newThumbImg, 0, 0, 0, 0, $newW, $newH, $width2, $height2);
    
    $imageFileClean = preg_replace("/Resource id #6/", "", $newname);
    //finally, save the image
    
    ImageJpeg ($newImg, $thumbDir . $newname);
     
    ImageDestroy ($newImg);
    ImageDestroy ($newThumbImg);

and im getting these errors:

Code:
[B]Notice[/B]:  Undefined variable: newThumbImg in [B]C:\wamp\www\includes\imageupload.php[/B] on line [B]288[/B]

[B]Warning[/B]:  imagecopyresized() expects parameter 2 to be resource, null given in [B]C:\wamp\www\includes\imageupload.php[/B] on line [B]288[/B]

[B]Notice[/B]:  Undefined variable: newThumbImg in [B]C:\wamp\www\includes\imageupload.php[/B] on line [B]296[/B]

[B]Warning[/B]:  imagedestroy() expects parameter 1 to be resource, null given in [B]C:\wamp\www\includes\imageupload.php[/B] on line [B]296

The same ^^ code is working for local images... so any1 can help?
[/B]
 

3 comments

A fixed code for resizing from remote images:

PHP:
<?php
$fileURL = 'http://www.wjunction.com/images/logo.png';
$pixels = getimagesize($fileURL); // Get image size in pixels for remote.
$width2 = $pixels[0];
$height2 = $pixels[1];
$thumbPixels = 90;
$newnam = 'wjsasdsa.png';
//Make a 90x90px thumbnail......
        //echo $fileURL;
        
        $newThumbImg = imagecreatefrompng($fileURL);
        
        if ($exten == "image/pjpeg" || $exten == "image/jpeg") { $newThumbImg = imagecreatefromjpeg($fileURL); }
        
        if ($exten == "image/x-png" || $exten == "image/png") { $newThumbImg = imagecreatefrompng($fileURL); }
        
        if ($exten == "image/gif") { $newThumbImg = imagecreatefromgif($fileURL); }
                       
        $ratio = $width2 / $height2; // calculate the ratio

        if ($ratio > 1 ) {
             $newW = $thumbPixels;
            $newH = $thumbPixels / $ratio;
    } else {
            
            $newH = $thumbPixels;
            $newW = $thumbPixels * $ratio;
    }
    
    //function for resize image.

    if ( function_exists ( 'imagecreatetruecolor' ) ) {

            $newImg = imagecreatetruecolor($newW, 25);
    } else {

            die("Error: Please make sure you have GD library ver 2+");
    }

    //the resizing is going on here!

    imagecopyresized($newImg, $newThumbImg, 0, 0, 0, 0, $newW, $newH, $width2, $height2);
    
    $imageFileClean = preg_replace("/Resource id #6/", "", $newname);
    //finally, save the image
    
    ImageJpeg ($newImg, "abcd.png");
     
    ImageDestroy ($newImg);
    ImageDestroy ($newThumbImg);  
?>
In Your Code:

PHP:
 //Make a 90x90px thumbnail......
        $fileURL = ($uploadDir . '/' . $newname);
        //echo $fileURL;
        
        if ($exten == "image/pjpeg" || $exten == "image/jpeg") { $newThumbImg = imagecreatefromjpeg($fileURL); }
        
        if ($exten == "image/x-png" || $exten == "image/png") { $newThumbImg = imagecreatefrompng($fileURL); }
        
        if ($exten == "image/gif") { $newThumbImg = imagecreatefromgif($fileURL); }
You are changing the fileurl before it could create image from the given url

Replace the above code with this code:

PHP:
 //Make a 90x90px thumbnail......
        $fileURL = ($uploadDir . '/' . $newname);
        //echo $fileURL;
        
        if ($exten == "image/pjpeg" || $exten == "image/jpeg") { $newThumbImg = imagecreatefromjpeg($url); }
        
        if ($exten == "image/x-png" || $exten == "image/png") { $newThumbImg = imagecreatefrompng($url); }
        
        if ($exten == "image/gif") { $newThumbImg = imagecreatefromgif($url); }

Note: Changing of $fileURL to $url to create image [imagecreatefromgif(imgurl)] so that it gets the image
 
Last edited:
This is what ive got now:

PHP:
        //Make a 90x90px thumbnail......
        $fileURL = ($uploadDir . '/' . $newname);
        //echo $fileURL;
        
        if ($exten == "image/pjpeg" || $exten == "image/jpeg") { $newThumbImg = imagecreatefromjpeg($url); }
        
        if ($exten == "image/x-png" || $exten == "image/png") { $newThumbImg = imagecreatefrompng($url); }
        
        if ($exten == "image/gif") { $newThumbImsge = imagecreatefromgif($url); }  
        
        if ($ratio > 1 ) {
             $newW = $thumbPixels;
            $newH = $thumbPixels / $ratio;
    } else {
            
            $newH = $thumbPixels;
            $newW = $thumbPixels * $ratio;
    }
    
    //function for resize image.

    if ( function_exists ( 'imagecreatetruecolor' ) ) {

            $newImg = imagecreatetruecolor($newW, 25);
    } else {

            die("Error: Please make sure you have GD library ver 2+");
    }

    //the resizing is going on here!

    imagecopyresized($newImg, $newThumbImg, 0, 0, 0, 0, $newW, $newH, $width2, $height2);
    
    $imageFileClean = preg_replace("/Resource id #6/", "", $newname);
    //finally, save the image
    
    ImageJpeg ($newImg, "abcd.png");
     
    ImageDestroy ($newImg);
    ImageDestroy ($newThumbImg);

im still getting errors:

Code:
[B]Notice[/B]:  Undefined variable: ratio in [B]C:\wamp\www\includes\imageupload.php[/B] on line [B]266[/B]

[B]Notice[/B]:  Undefined variable: ratio in [B]C:\wamp\www\includes\imageupload.php[/B] on line [B]272[/B]

[B]Warning[/B]:  imagecreatetruecolor() [[URL="http://localhost/function.imagecreatetruecolor"]function.imagecreatetruecolor[/URL]]: Invalid image dimensions in [B]C:\wamp\www\includes\imageupload.php[/B] on line [B]279[/B]

[B]Notice[/B]:  Undefined variable: newThumbImg in [B]C:\wamp\www\includes\imageupload.php[/B] on line [B]287[/B]

[B]Warning[/B]:  imagecopyresized() expects parameter 1 to be resource, boolean given in [B]C:\wamp\www\includes\imageupload.php[/B] on line [B]287[/B]

[B]Warning[/B]:  imagejpeg() expects parameter 1 to be resource, boolean given in [B]C:\wamp\www\includes\imageupload.php[/B] on line [B]292[/B]

[B]Warning[/B]:  imagedestroy() expects parameter 1 to be resource, boolean given in [B]C:\wamp\www\includes\imageupload.php[/B] on line [B]294[/B]

[B]Notice[/B]:  Undefined variable: newThumbImg in [B]C:\wamp\www\includes\imageupload.php[/B] on line [B]295[/B]

[B]Warning[/B]:  imagedestroy() expects parameter 1 to be resource, null given in [B]C:\wamp\www\includes\imageupload.php[/B] on line [B]295

any ideas?
[/B]
 
apologies, Soft2050.

it was working, i just didnt realize it was saving to the root, anyways all is cool now, slightly modified............ thx
 
Status
Not open for further replies.

About the author

C
Banned · Joined
901
Messages
17
Reactions
18
Points
Grow your business

Advertise on WJunction

Connect with an established international community of webmasters, hosting providers, developers and online entrepreneurs.

  • Premium & Premium+ Sticky
  • Dedicated section banners
  • Site-wide banner advertising
Explore advertising options
Back
Top Bottom