imagecreatefromstring() - gd函数(图像处理)
imagecreatefromstring()
(PHP 4 >= 4.0.4, PHP 5, PHP 7)
从字符串中的图像流新建一图像
说明
imagecreatefromstring(string $image): resourceimagecreatefromstring()返回一个图像标识符,其表达了从给定字符串得来的图像。图像格式将自动检测,只要 PHP 支持:JPEG,PNG,GIF,WBMP 和 GD2。
参数
$imageA string containing the image data.
返回值
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.
范例
imagecreatefromstring()example
以上例程的输出类似于:
参见
imagecreatefromjpeg()
由文件或 URL 创建一个新图象。imagecreatefrompng()
由文件或 URL 创建一个新图象。imagecreatefromgif()
由文件或 URL 创建一个新图象。imagecreatetruecolor()
新建一个真彩色图像
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:
My site allows anonymous uploads to a web-accessible location (that will execute a script if it finds one). Naturally, I need to verify that only harmless content is accepted. I am expecting only images, so I use:
[Editor's note: BMP will be supported as of PHP 7.2.0.] In case it's not obvious from the lack of "imagecreatefrombmp()" in GD, this function cannot handle plain old BMP files either.
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.
An easy example to help understanding this function... The function will try to auto-determine file format (jpg, gif, png....), and will return false if fails.
imagecreatefromstring does not appear to support WebP images (tested on PHP 7.2.10, with GD 2.1.0 and GD WebP support enabled)
Many websites add, during the conversion from .jpg / .png to base64 some words before the base64 string : "data:image/png;base64," If you let them, the PHP function will return an error "Data is not in a recognized format in". If you have this situation : $image_string=str_replace("data:image/png;base64,","",$image_string); $image_string = base64_decode($image_string); $img = imagecreatefromstring($image_string);
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 Visco IM: paulsidekick Created: 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; } }
Generating graphs in an external program (in this case, gnuplot) may cause problems. This is a solution hacked together in the late hours of night. [/PHP]
Here is the code I did to create a thumbnail image from the database blob field. The trick is to use "imagecreatefromstring()" to create an image file. Jack Shieh
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) 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
I'm trying to get the imagecreatefromstring to work with GIFs. Of course, it won't. I've read the tips but can't get them to work either. The following is what I tried, based on above tips: --- header('Content-Type: image/gif'); header('Content-Disposition: inline; filename=file.gif'); $temp = tmpfile(); fwrite($temp, $line['image']); $src_img = imagecreatefromgif($temp); fclose($temp); // this removes the file $dst_img = imagecreatetruecolor(100, 100); imagecopyresampled($dst_img, $src_img, 0,0,0,0, 100,100, imagesx($src_img), imagesy($src_img)); imagegif($dst_img); --- where $line['image'] is the gif as taken from my MySQL database... If anyone that has been able to make something like this work could give me a working piece of code I'd be really greatful! I would be great if the tempfile could be excluded too... Below is a working piece of code for jpeg: --- header('Content-Type: image/jpeg'); header('Content-Disposition: inline; filename=file.jpg'); $src_img = imagecreatefromstring($line['image']); $dst_img = imagecreatetruecolor(100, 100); imagecopyresampled($dst_img, $src_img, 0,0,0,0, 100,100, imagesx($src_img), imagesy($src_img)); imagejpeg($dst_img);
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)