Home >Backend Development >PHP Tutorial >Detailed explanation of the usage of PHP image processing function imagecopyresampled
The example in this article describes the usage of PHP image processing function imagecopyresampled. Share it with everyone for your reference, the details are as follows:
Syntax
bool imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )
Parameters
Return TRUE on success, or return on failure FALSE.
Case
Case (image cropping):
<?php $targ_w = $targ_h = 150; // 设置目标宽度与高度 $jpeg_quality = 90; // 图片质量90,满分为100 $src = 'demo_files/pool.jpg'; // 被处理的图片 $img_r = imagecreatefromjpeg($src); // 获取原图 $dst_r = ImageCreateTrueColor( $targ_w, $targ_h ); // 获取新图 imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'], $targ_w,$targ_h,$_POST['w'],$_POST['h']); // 目标图 源图 目标X坐标点 目标Y坐标点 源的X坐标点 源的Y坐标点 目标宽度 目标高度 源图宽度 源图高度 header('Content-type: image/jpeg'); imagejpeg($dst_r,null,$jpeg_quality); // 输出图象到浏览器或文件 ?>
Case 2 (resampling):
<?php // 源文件 $filename = '1.jpg'; // 设置最大宽高 $width = 400; $height = 400; // Content type header('Content-Type: image/jpeg'); // 获取新尺寸 list($width_orig, $height_orig) = getimagesize($filename); $ratio_orig = $width_orig/$height_orig; if ($width/$height > $ratio_orig) { $width = $height*$ratio_orig; } else { $height = $width/$ratio_orig; } // 重新取样 $image_p = imagecreatetruecolor($width, $height); $image = imagecreatefromjpeg($filename); imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig); // 输出 imagejpeg($image_p, null, 100); ?>
Attached are three ideas for uploading pictures
1. Select the picture, submit the form, the server will handle the upload uniformly, and save the path
2 .Select the picture, upload, get the path, submit the form, save the path
3.Select the picture, upload it to the server, get the picture from the server through some way, save it locally
Hope this article The above will be helpful to everyone in PHP programming.
For more detailed usage of PHP image processing function imagecopyresampled and related articles, please pay attention to PHP Chinese website!