Asked  2 Years ago    Answers:  5   Viewed   88 times

I know of imagecreatefromgif(), imagecreatefromjpeg(), and imagecreatefrompng() but is there a way to create an image resource (for png preferably) from a url of any type of valid image? Or do you have to determine the file type and then use the appropriate function?

When I say url I mean something like http://sample.com/image.png, not a data url

 Answers

5

Maybe you want this:

$jpeg_image = imagecreatefromfile( 'photo.jpeg' );
$gif_image = imagecreatefromfile( 'clipart.gif' );
$png_image = imagecreatefromfile( 'transparent_checkerboard.PnG' );
$another_jpeg = imagecreatefromfile( 'picture.JPG' );
// This requires you to remove or rewrite file_exists check:
$jpeg_image = imagecreatefromfile( 'http://example.net/photo.jpeg' );
// SEE BELOW HO TO DO IT WHEN http:// ARGS IS NEEDED:
$jpeg_image = imagecreatefromfile( 'http://example.net/photo.jpeg?foo=hello&bar=world' );

Here's how it's done:

function imagecreatefromfile( $filename ) {
    if (!file_exists($filename)) {
        throw new InvalidArgumentException('File "'.$filename.'" not found.');
    }
    switch ( strtolower( pathinfo( $filename, PATHINFO_EXTENSION ))) {
        case 'jpeg':
        case 'jpg':
            return imagecreatefromjpeg($filename);
        break;

        case 'png':
            return imagecreatefrompng($filename);
        break;

        case 'gif':
            return imagecreatefromgif($filename);
        break;

        default:
            throw new InvalidArgumentException('File "'.$filename.'" is not valid jpg, png or gif image.');
        break;
    }
}

With some small modifications to switch same function is ready for web url's:

    /* if (!file_exists($filename)) {
        throw new InvalidArgumentException('File "'.$filename.'" not found.');
    } <== This needs addiotional checks if using non local picture */
    switch ( strtolower( array_pop( explode('.', substr($filename, 0, strpos($filename, '?'))))) ) {
        case 'jpeg':

After that you can use it with http://www.tld/image.jpg:

$jpeg_image = imagecreatefromfile( 'http://example.net/photo.jpeg' );
$gif_image = imagecreatefromfile( 'http://www.example.com/art.gif?param=23&another=yes' );

Some proofs:

As you can read from official PHP manual function.imagecreatefromjpeg.php GD allows loading images from URLs that is supported by function.fopen.php, so there is no need to fetch image first and save it to file, and open that file.

Thursday, September 29, 2022
5

Try the imagecopyresized function,
which is built in,
need not to re-compile (your share hosting will be happy),
and provide almost simple feature for image processing

Jquery is clients javascript library,
it does not help with image processing

Tuesday, December 13, 2022
 
qiao
 
4

run

 $data = getimagesize("/home/logo.jpg");
 var_dump($data);

and make sure that the MIME TYPE of the image is image/jpeg

another reason might be that the path to the file is not true (maybe you need to remove the starting "/" and leave only imagecreatefromjpeg("home/logo.jpg") [if the home directory is in the same level as your php file - than you should]

Tuesday, October 4, 2022
 
4

Have you tried first converting it into a node.js. Buffer? (this is the native node.js Buffer interface, whereas ArrayBuffer is the interface for the browser and not completely supported for node.js write operations).

Something along the line of this should help:

all= fs.createWriteStream("out."+imgtype);

for(i=0; i<end; i++){
    var buffer = new Buffer( new Uint8Array(picarray[i]) );
    all.write(buffer);
}
all.end();
Tuesday, August 23, 2022
5

The following works fine when executed at the end of the code posted in the question:

String url = imagesService.getServingUrl(newBlobKey)

The URL can then be used to scale and crop the new image as described in the docs http://code.google.com/appengine/docs/java/images/overview.html#Transforming_Images_from_the_Blobstore

Sunday, December 11, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 

Browse Other Code Languages