imagecreatefromstring

(PHP 4 >= 4.0.4, PHP 5)

imagecreatefromstring — Create a new image from the image stream in the string

Descrierea

resourceimagecreatefromstring
( string$image
)

imagecreatefromstring() returns an image identifier
representing the image obtained from the given image.
These types will be automatically detected if your build of PHP supports
them: JPEG, PNG, GIF, WBMP, and GD2.

Parametri

image

A string containing the image data.

Valorile întoarse

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.

While downloading images from internet, it's easiest to let php decide what is the file type. So, forget using imagecreatefromjpg, imagecreatefromgif and imagecreatefrompng. Instead, this is the way to go:

I know a lot of people have been having trouble using images from the Sprint PPC6700 with GD. The jpeg library always thows an error about 16 bytes of extraneous data. I decided to poke around in a hex editor and found those extraneous bytes occur right at the end of the exif data.

By removing this extraneous data, images can easily be manipulated by GD and the EXIF extension as a normal images. This also removes any problems using the images with GIMP which must rely on the same jpeg libraries.

Here is a function that will check to see if an image is from the PPC6700 and then remove the extraneous data for you. The result can successully be used with imagecreatefromstring

Author: Paul ViscoIM: paulsidekickCreated: 2-12-06

function checkFixPPC6700($orig){

//get the file contents $data = file_get_contents($orig);

//if its a PPC 6700 image cut out the extraneous 16 bits if(strstr($data, "\x41\x70\x61\x63\x68\x65\x00\x48")){//this next line can all be one string I split it up so the form on php.net would accept it $bad_data ="\x00\x10\x4A\x46" . "\x49\x46\x00\x01\x01" . "\x00\x00\x01\x00\x01\x00\x00"; return substr_replace($data, "", strpos($data, $bad_data), strlen($bad_data)); } else { //if not from a PPC 6700 return data unaltered return $data; }

I run a blogging site that allowed users to publish images from their cell phones. For some reason, the nokia 3220 produces jpegs with extrandeous data before the EOF 0xFFF9

I wrote this class to allow you to fix the images so that they can be used with GD to resize and manipulate. Without applying the fix both GD and gimp report errors for the file. If basically keeps eating bytes from the junk part till the file is a valid jpeg. Here is an example bunk pic written by a nokia 3220 that you cna test it with http://www.shawnrider.com/moblog/cache/0854747001121624103.jpg

/*Author: Paul ViscoHompage: http://www.elmwoodstrip.com?u=paulAIM: paulsidekickNotes: The file allows you fix the jpegs created by the Nokia 3220 picture phone so that they can be manipulated using the GD library with PHP.

Usage: Simply instanitiate the class and then call the fix method for each image.e.g. $nokia = new nokia;$nokia->debug ="y";$nokia->fix("yourpic.jpg");*/

//load the file into a string $this->str = file_get_contents($this->file);

//test to see if it is a nokia image or if it has been corrupted previously if (substr_count($this->str, chr(0x28).chr(0x36).chr(0x28).chr(0x2B)) == 0 || @imagecreatefromstring($this->str)) { if ($this->debug =="y") { echo "\n<br />".$this->file." is not a corrupted Nokia pic"; }

return true; }

//make a directory for fixed images if it doesn't exist and is specified if(!empty($this->dir_name) && !is_dir($this->dir_name)) { @mkdir($this->dir_name, 0777); }

//strip out the funk e crap from the file $this->eat($this->str);

//write the file back to itself file_put_contents($this->dir_name.$this->file, $this->str);

//return true for fixed return true;

}

function eat() {

//check the image $this->img = @imagecreatefromstring($this->str);

//if the image doesn't work then keep striping out crap while(!$this->img) { //cut off the bad bytes one by one $this->str= substr_replace($this->str, "", -3, -2);

The only way I managed to resolve the loading of image files from file data either on local site or from off-site protocols is as follows. I hope this saves someone else the two hours of debugging and looking for good examples.[PHP]

I use dynamically generated images that require a little touch-up before being displayed. So essentially I do some base work, then store the images in a memory cache (APC), reload the images again from the cache later on "into" GD, do final processing and then display the image. Since I wanted to avoid a lot of disc access I used the output buffering functions:

I found out that in PHP 4.3.2 and 4.3.4 that the 'imagecreatefromstring' function use a lot of memory when working with big image...Try this piece of code with a big image (like 1Meg or more)<?php$loadFile = "http://bigimage.jpg";//Try with a big image $im = imagecreatefromstring(file_get_contents($loadFile));imagejpeg($im);?>

With a 400k image. I run with a 16M Mem_limit but for a 1.4M image, the mem_limit required jump to more that 32M

So you guys don't spend an hour trying to figure out why your script keeps running out of memory when you're using this or the other imagecreatefrom functions. GD uncompresses the image when you use these functions, and this can lead to your script running out of memory.

If you download a rawimage save it on your computer to jpeg so the file size comes down, GD will automatically convert it to the raw and you can possibly run out of memory.