After using the php GD library to process the image, you can only use imagejpeg() to output or write the image to a file. In many cases, there is no need to do this. For example, if you want to save the image into the database, you need to write the image into a variable and save it with ob_start( ) Enable cache ob_get_contents() to get the cache and write the image to the variable
The code is as follows
代码如下 |
复制代码 |
$imgPath ="图片地址" ;
//获得图片信息 $imgPath 可以为远程地址
list( $srcWidth, $srcHeight, $type ) = getimagesize( $imgPath );
...
switch( $type ) {
case 1: $imgCreate = 'ImageCreateFromGIF'; break;
case 2: $imgCreate = 'ImageCreateFromJPEG'; break;
case 3: $imgCreate = 'ImageCreateFromPNG'; break;
default: return false;
}
$orig = $imgCreate( $imgPath );
...
//开启缓存
ob_start();
//生成图片
switch ($type)
{
case 1: imagegif($orig); break;
case 2: imagejpeg($orig); break; // best quality
case 3: imagepng($orig); break; // no compression
default: echo ''; break;
}
//将图片存入变量
$imageCode = ob_get_contents();
ob_end_clean();
|
|
Copy code
|
|
$imgPath ="image address" ;
//Get image information $imgPath can be a remote address
list( $srcWidth, $srcHeight, $type ) = getimagesize( $imgPath );
...
switch( $type ) {
case 1: $imgCreate = 'ImageCreateFromGIF'; break;
case 2: $imgCreate = 'ImageCreateFromJPEG'; break;
case 3: $imgCreate = 'ImageCreateFromPNG'; break;
default: return false;
}
$orig = $imgCreate( $imgPath );
...
//Enable caching
ob_start();
//Generate pictures
switch ($type)
{
case 1: imagegif($orig); break;
case 2: imagejpeg($orig); break; // best quality
case 3: imagepng($orig); break; // no compression
default: echo ''; break;
}
//Save the image into a variable
$imageCode = ob_get_contents();
ob_end_clean();
I don’t recommend saving the image in a variable. This would be a waste of resources. It’s also fun to test here.
http://www.bkjia.com/PHPjc/629003.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/629003.htmlTechArticleAfter using the php GD library to process the image, you can only use imagejpeg() to output or write the image to a file. Many times There is no need to do this. For example, if you want to save the image into the database, you need to write the image...