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 533125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:28:36+00:00 2026-05-13T09:28:36+00:00

I am trying to make a function in PHP that will allow me to

  • 0

I am trying to make a function in PHP that will allow me to enter basically any URL and then runs some functions on it just as if a user was uploading on my server. SO I will resize and make some thumbnails but I need help just getting the image in a state that I can run my other codes on it. Another user on this site helped me get started with ImageCreateFromString() and file_get_contents()

Please note this code is missing a lot of stuff I am aware of, I am just trying to get the basic function working and then I will add in all the security measures

I tried this code below using a URL like this with the photo URL added to my script url:

http://example.com/friendproject2/testing/photos/fromurl/?url=http://a0.twimg.com/a/1262802780/images/twitter_logo_header.png

But it shows nothing and not even an error

function getphotoURL($url){
    if(isset($url) && $url != 'bad') {
        $image = ImageCreateFromString(file_get_contents($url));
        if (is_resource($image) === true){
            echo 'The URL of the image we fetch is :' .$url. '<BR><BR>';
            //show image
            header('Content-Type: image/jpeg');
            imagejpeg($image, null, 100);
            imagedestroy($image); 
            imagedestroy($image); 
            // image is valid, do your magic here
        }else{
            // not a valid image, show error
            echo 'error getting URL photo from ' .$url;
        }
    }else{
        //url was empty
        echo 'The URL was not passed into our function';
    }
}
?>

###### UPDATE #####

It seems it was a simple error on my part, something simple as checking for a POST request instead of a GET request, here is my new code below.

I have a couple of issues,

1) I am using imagejpeg($image, null, 100); and I am wondering, should I be using something else? Does it require the source image to be a jpg or will it work with any image? I need to allow the main types (jpg, jpeg, gif, png)

2) same as above question but for when showing the image on screen I have header set to this: header(‘Content-Type: image/jpeg’); should it not be jpg for other type of images?

3) Is there a way that I can make sure that the source URL passed in is an actual image and do whatever I want if it is not a image, like show my own error or do my own code once it detect that the URL is not a valid image url

<?PHP
// run our function
if(isset($_GET['url']) && $_GET['url'] != "") {
    getphotoURL($_GET['url'],'no');
}


function getphotoURL($url, $saveimage = 'yes'){
    if(isset($url) && $url != '') {
        $image = imagecreatefromstring(file_get_contents($url));
        if (is_resource($image) === true){
            if($saveimage === 'yes'){
                // resize image and make the thumbs code would go here if we are saving image:
                // resize source image if it is wider then 800 pixels
                // make 1 thumbnail that is 150 pixels wide
            }else{
                // We are not saving the image show it in the user's browser
                header('Content-Type: image/png');
                imagejpeg($image, null, 100);
                imagedestroy($image); 
            }
        }else{
            // not a valid resource, show error
            echo 'error getting URL photo from ' .$url;
        }
    }else{
        // url of image was empty
        echo 'The URL was not passed into our function';
    }
}
?>
  • 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-13T09:28:36+00:00Added an answer on May 13, 2026 at 9:28 am

    In response to your new questions:

    1) I am using imagejpeg($image, null,
    100); and I am wondering, should I be
    using something else? DOes it require
    the source image to be a jpg or will
    it work wiht any image? I need to
    allow the main types (jpg, jpeg, gif,
    png)

    Well, php.net says, “imagejpeg() creates a JPEG file from the given image”. But the important part is this, “An image resource, returned by one of the image creation functions, such as imagecreatetruecolor().”. And your using “imagecreatefromstring() returns an image identifier representing the image obtained from the given data . These types will be automatically detected if your build of PHP supports them: JPEG, PNG, GIF, WBMP, and GD2.”
    So, that should be ok.

    2) same as above question but for when
    showing the image on screen I have
    header set to this:
    header(‘Content-Type: image/jpeg’);
    should it not be jpg for other type of
    images?

    The header should be of type jpg – If that’s the file type, then you’re correct.

    3) Is there a way that I can make sure
    that the source URL passed in is an
    actual image and do whatever I want if
    it is not a image, like show my own
    error or do my own code once it detect
    that the URL is not a valid image url

    Yeah – Instead of doing:

    $image = ImageCreateFromString(file_get_contents($url));
    

    You could do:

    $image = imagecreatefromjpeg($url);
    if (!$image) echo "error"; 
    

    imagecreatefromjpeg() returns an image
    identifier representing the image
    obtained from the given filename.

    But really, what you have is fine.


    Does it display the error message

        echo 'The URL was not passed into our function';
    

    Or nothing at all?

    If the error messaging is being displayed, possible the check === is failing:

    An image resource will be returned on
    success. FALSE is returned if the
    image type is unsupported, the data is
    not in a recognised format, or the
    image is corrupt and cannot be loaded.

    Also, do you have error logging maxed out on your development server? That way you can see any possible warnings being thrown?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 316k
  • Answers 316k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer In the exact case you're showing, there is no difference.… May 13, 2026 at 11:25 pm
  • Editorial Team
    Editorial Team added an answer You have to give it the class "required" as well.… May 13, 2026 at 11:25 pm
  • Editorial Team
    Editorial Team added an answer Did you properly escaped your data when inserting/updating with your… May 13, 2026 at 11:25 pm

Related Questions

Thanks for reading. I'm a bit new to jQuery, and am trying to make
I am trying to reference a JavaScript file, containing a table sort function, in
I have a pretty large site and every page is built from several included
I am trying to make a simple redirector controller in CakePHP. I'd like the
I'm fairly new to php and I am trying to figure how do I

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.