Home  >  Article  >  Backend Development  >  PHP single row from scratch (18) image processing_PHP tutorial

PHP single row from scratch (18) image processing_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:30:10710browse

1. Open an existing picture

<?php
header("Content-type:image/jpeg"); 
$img=imagecreatefromjpeg("cc.jpg");
imagejpeg($img);
imagedestroy($img);
?>
The parameter of the function imagecreatefromjpeg() is the path where the file is located, and the return value is the resource identifier of the image pointed to by the parameter. This function creates a new image through an existing image, rather than simply opening the original image itself. If the image suffix name .jpg is forcibly changed to .png, even if the function imagecreatefrompng() is used, the file cannot be opened because the image is still in jpg format.

2. Get the relevant attributes of the image

<?php
$img=imagecreatefromjpeg("cc.jpg");
$x=imagesx($img);
$y=imagesy($img);
echo "图片cc.jpg的宽为:<b>$x</b> pixels";
echo "<br/>";
echo "<br/>";
echo "图片cc.jpg的高为:<b>$y</b> pixels";
?>

In addition, through a function getimagesize() that does not belong to the GD library, you can obtain the size of the image and other related attributes. The syntax of this function is as follows:

array getimagesize(string $filename [, array &imageinfo])

<?php
$img_info=getimagesize("cc.jpg");
for($i=0;$i<4;++$i)
{
	echo $img_info[$i];
	echo "<br/>";	
	}
?>
The third element is the format of the image, and its value meaning is as follows:

1: Indicates that the image is in GIF format

2: Indicates that the image is in JPG format

3: Indicates that the image is in PNG format

4: Indicates that the image is in SWF format

5: Indicates that the image is in PSD format

6: Indicates that the image is in BMP format

<?php
$pic_name="ee.png";
$pic_size=getimagesize($pic_name);
?>
<img  src="http://blog.csdn.net/u014761013/article/details/<?php echo $pic_name; ? alt="PHP single row from scratch (18) image processing_PHP tutorial" >"<?php echo $pic_size[3]; ?>>

3. Add watermark effect to pictures

·Get the width and height of the image to be watermarked

·Determine whether the image size meets the watermark text size

·Determine the position of the watermark effect in the image

·Set the color blending mode of the image

·Generate watermark effect

·Release resources

<?php 
function makeimagewatermark($image,$pos,$water_text,$font_size,$color)
{
	$font_type="c://WINDOWS//Fonts//SIMYOU.TTF";
	if(!empty($image)&& file_exists($image))
	{
		$img_info=getimagesize($image);
		$g_w=$img_info[0];
		$g_h=$img_info[1];
		switch($img_info[2])
		{
			case 1:
			$img=imagecreatefromgif($image);
			break;
			case 2:
			$img=imagecreatefromjpeg($image);
			break;
			case 3:
			$img=imagecreatefrompng($image);
			break;
			default:
			die("Format Wrong");			
			}		
		}
	else 
	{
		die("Not exists!");		
		}	
	$temp=imagettfbbox(ceil($font_size*2.5),0,$font_type,$water_text);
	$w=$temp[2]-$temp[6];
	$h=$temp[3]-$temp[7];
	if(($g_w<$w) || ($g_h<$h))
	{
		echo "Too small!";
		return;		
		}	
switch($pos)
{
	case 0:
	$pos_x=rand(0,($g_w-$w));
	$pos_y=rand(0,($g_h-$h));
	break;
	case 1:
	$pos_x=0;
	$pos_y=0;
	break;
	case 2:
	$pos_x=($g_w-$w)/2;
	$pos_y=($g_h-$h)/2;
	break;
	case 3:
	$pos_x=$g_w-$w;
	$pos_y=$g_h-$h;
	break;
	default:
	$pos_x=rand(0,($g_w-$w));
	$pos_y=rand(0,($g_h-$h));
	break;	
	}		
	imagealphablending($img,true);//设置图像混色模式
	
	if(!empty($color) && (strlen($color)==7))
{
	$R=hexdec(substr($color,1,2));
	$G=hexdec(substr($color,3,2));
	$B=hexdec(substr($color,5));	
	}	
	else 
	{
		die("Format wrong!");
		
		}
		$text_color=imagecolorallocate($img,$R,$G,$B);		
		imagettftext($img,$font_size,0,$pos_x,$pos_y,$text_color,$font_type,$water_text);		
		switch($img_info[2])
		{
			case 1 :
			imagegif($img,$image);
			break;
			case 2 :
			imagejpeg($img,$image);
			break;
			case 3:
			imagepng($img,$image);
			break;
			default:
			die("Formate unSupport!");			
			}
	imagedestroy($img);	
	}
if(isset($_FILES) && !empty($_FILES['userfile'])&& $_FILES['userfile']['size']>0)
{$uploadfile="./".time()."_".$_FILES['userfile']['name'];
if(copy($_FILES['userfile']['tmp_name'],$uploadfile))
{
    makeimagewatermark($uploadfile,2,"Photo by Mac",16,"#43042A");
	echo "<img  src=\"".$uploadfile."\" border=\"0\" alt="PHP single row from scratch (18) image processing_PHP tutorial" >";	
	}
else
{
	echo "uploadWrong!<br/>";
	}
}
?>
<html>
<head>
<title>
19.9.php
</title>
</head>
<body>

</body>
</html>

4. Generate thumbnails of existing images

<?php 
header("Content-type:image/jpeg");
$img_name="cc.jpg";
$src_img=imagecreatefromjpeg($img_name);
$ow=imagesx($src_img);
$oh=imagesy($src_img);
$nw=round($ow*200.0/$ow);
$nh=round($oh*200.0/$oh);
$desc_img=imagecreate($nw,$nh);
imagecopyresized($desc_img,$src_img,0,0,0,0,$nw,$nh,$ow,$oh);
imagejpeg($desc_img);
imagedestroy($desc_img);
imagedestroy($src_img);
?>
The first and second parameters are the identifiers of the target image and the original image respectively. The next 4 parameters are the coordinates of the copy positions of the target image and the original image. The last 4 parameters are the coordinates of the target image and the original image. The width and height of the copied area.
! ! Use the function imagecopyresampled() function

<?php 
header("Content-type:image/jpeg");
$img_name="cc.jpg";
$percent=0.2;
$src_img=imagecreatefromjpeg($img_name);
$ow=imagesx($src_img);
$oh=imagesy($src_img);
$nw=$ow*$percent;
$nh=$oh*$percent;
$desc_img=imagecreatetruecolor($nw,$nh);
imagecopyresampled($desc_img,$src_img,0,0,0,0,$nw,$nh,$ow,$oh);
imagejpeg($desc_img);
imagedestroy($desc_img);
imagedestroy($src_img);
?>




www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/767111.htmlTechArticle1. Open the existing image function imagecreatefromjpeg(). The parameter is the path to the file, and the returned image is the image pointed to by the parameter. resource identifier. This function creates a new image from an existing image...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn