Asked  7 Months ago    Answers:  5   Viewed   35 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

43

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.

Wednesday, March 31, 2021
 
PHLAK
answered 7 Months ago
39

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

Wednesday, March 31, 2021
 
juananrey
answered 7 Months ago
62

"Tracking" is a similar term for how tight or loose text is set. You might have better luck googling for that, such as this result.

Wednesday, March 31, 2021
 
smiggle
answered 7 Months ago
44

The PHP part actually works for me, though this could be replaced:

ob_start();
imagepng($im);
$data = ob_get_clean();
file_put_contents($to,$data);
imagedestroy($im);

with this:

imagepng($im, $to);
imagedestroy($im);

But I'm not positive if that's the issue, since your existing code worked for me. So try checking if the $paths is a valid directory?

UPDATE

nothing seems to be happening

Could it be because you didn't echo (or output something) after the image is created? Well I mean, the image may have been successfully (re-)created, but your PHP/JS script doesn't "tell" you of the status; hence you see "nothing". Try echo-ing something like this:

if (isset($_POST['make_pic'])) {
  ...
  imagedestroy($im);

  echo 'Success!';
}

And then use this JS:

$('#picform').ajaxForm({
  success: function(res){
    alert(res);
  }
});

(Update #2: I intentionally removed the "Additional Note" part, but you can always see the answer's revisions to find that part.)

Wednesday, March 31, 2021
 
Kwadz
answered 7 Months ago
57

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();
Saturday, October 16, 2021
 
McDowell
answered 1 Week ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :