php影像處理函數imagecopyresampled用法詳解

高洛峰
發布: 2023-03-04 09:42:02
原創
2294 人瀏覽過

本文實例講述了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 )
登入後複製

參數

php影像處理函數imagecopyresampled用法詳解

成功時返回 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); // 输出图象到浏览器或文件
?>
登入後複製

   

案例二(重新取樣):

<?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);
?>
登入後複製

   

案例二(重新取樣):

rrreee

   

案例二(伺服器統一處理上傳,保存路徑

2.選擇圖片,上傳,獲取路徑,提交表單,保存路徑

3.選擇圖片,上傳到伺服器,透過某種途徑獲取到伺服器的圖片,保存到本地

希望本文所述對大家PHP程式設計有幫助。

更多php影像處理函數imagecopyresampled用法詳解相關文章請關注PHP中文網!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!