Rumah > pembangunan bahagian belakang > tutorial php > php图像处理函数imagecopyresampled用法详解

php图像处理函数imagecopyresampled用法详解

高洛峰
Lepaskan: 2023-03-04 09:42:02
asal
2466 orang telah melayarinya

本文实例讲述了php图像处理函数imagecopyresampled用法。分享给大家供大家参考,具体如下:

语法

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 )
Salin selepas log masuk

参数

参数

成功时返回 TRUE, 或者在失败时返回 FALSE。

案例

案例(图像裁减):

<?php
  $targ_w = $targ_h = 150; // 设置目标宽度与高度
  $jpeg_quality = 90; // 图片质量90,满分为100
  $src = &#39;demo_files/pool.jpg&#39;; // 被处理的图片
  $img_r = imagecreatefromjpeg($src); // 获取原图
  $dst_r = ImageCreateTrueColor( $targ_w, $targ_h ); // 获取新图
  imagecopyresampled($dst_r,$img_r,0,0,$_POST[&#39;x&#39;],$_POST[&#39;y&#39;],
  $targ_w,$targ_h,$_POST[&#39;w&#39;],$_POST[&#39;h&#39;]); // 目标图 源图 目标X坐标点 目标Y坐标点 源的X坐标点 源的Y坐标点 目标宽度 目标高度 源图宽度 源图高度
  header(&#39;Content-type: image/jpeg&#39;);
  imagejpeg($dst_r,null,$jpeg_quality); // 输出图象到浏览器或文件
?>
Salin selepas log masuk

案例二(重新取样):

<?php
// 源文件
$filename = &#39;1.jpg&#39;;
// 设置最大宽高
$width = 400;
$height = 400;
// Content type
header(&#39;Content-Type: image/jpeg&#39;);
// 获取新尺寸
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);
?>
Salin selepas log masuk

   

附上上传图片的三种思路

1.选择图片,提交表单,服务器统一处理上传,保存路径

2.选择图片,上传,获取路径,提交表单,保存路径

3.选择图片,上传到服务器,通过某种途径获取到服务器的图片,保存到本地

希望本文所述对大家PHP程序设计有所帮助。

更多php图像处理函数imagecopyresampled用法详解相关文章请关注PHP中文网!

Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan