Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3846546
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T16:24:52+00:00 2026-05-19T16:24:52+00:00

I use this script to download and resize a remote image. In the resize

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T16:24:52+00:00Added an answer on May 19, 2026 at 4:24 pm

    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))){
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My PHP knowledge is limited and I am trying to implement http://www.tutorialchip.com/php-download-file-script/ this script
During development, I use this to load the latest jQuery: <script src=http://www.google.com/jsapi></script> <script type=text/javascript>
i use this script to download images from the same html page. But if
I am using jQuery auto-complete. I have download this script from http://code.google.com/p/jquery-autocomplete/ . I
I used this great script for an older project to download images to use
I use this script to equalize heights of elements: (function ($) { $.fn.autoheight =
I use this script to call a javascript from codebehind. ClientScript.RegisterStartupScript(this.GetType(), Exist, <script language='javascript'>ConfirmRedirect('
I use this script to show an additional language menu additional, which works great
I use this script: $(document).ready(function() { $page = jQuery.url.attr(file); alert(page); }); but I get
I used to use this script for jquery email obfuscation: $(.replaceAt).replaceWith(@); $(.obfuscate).each(function () {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.