PHP基於GD庫實作產生圖片縮圖函數的方法

墨辰丷
發布: 2023-03-27 06:02:01
原創
1303 人瀏覽過

這篇文章主要介紹了PHP基於GD庫實現的生成圖片縮圖函數,涉及php針對圖片屬性相關操作技巧,需要的朋友可以參考下

本文實例講述了PHP基於GD庫實現的生成圖片縮圖函數。分享給大家供大家參考,具體如下:

<?php
/**
 * 生成缩略图函数(支持图片格式:gif、jpeg、png和bmp)
 * @author ruxing.li
 * @param string $src   源图片路径
 * @param int  $width  缩略图宽度(只指定高度时进行等比缩放)
 * @param int  $width  缩略图高度(只指定宽度时进行等比缩放)
 * @param string $filename 保存路径(不指定时直接输出到浏览器)
 * @return bool
 */
function mkThumbnail($src, $width = null, $height = null, $filename = null) {
  if (!isset($width) && !isset($height))
    return false;
  if (isset($width) && $width <= 0)
    return false;
  if (isset($height) && $height <= 0)
    return false;
  $size = getimagesize($src);
  if (!$size)
    return false;
  list($src_w, $src_h, $src_type) = $size;
  $src_mime = $size[&#39;mime&#39;];
  switch($src_type) {
    case 1 :
      $img_type = &#39;gif&#39;;
      break;
    case 2 :
      $img_type = &#39;jpeg&#39;;
      break;
    case 3 :
      $img_type = &#39;png&#39;;
      break;
    case 15 :
      $img_type = &#39;wbmp&#39;;
      break;
    default :
      return false;
  }
  if (!isset($width))
    $width = $src_w * ($height / $src_h);
  if (!isset($height))
    $height = $src_h * ($width / $src_w);
  $imagecreatefunc = &#39;imagecreatefrom&#39; . $img_type;
  $src_img = $imagecreatefunc($src);
  $dest_img = imagecreatetruecolor($width, $height);
  imagecopyresampled($dest_img, $src_img, 0, 0, 0, 0, $width, $height, $src_w, $src_h);
  $imagefunc = &#39;image&#39; . $img_type;
  if ($filename) {
    $imagefunc($dest_img, $filename);
  } else {
    header(&#39;Content-Type: &#39; . $src_mime);
    $imagefunc($dest_img);
  }
  imagedestroy($src_img);
  imagedestroy($dest_img);
  return true;
}
$result = mkThumbnail(&#39;./IMG_3324.JPG&#39;, 147, 147);
登入後複製

#:記得先開啟GD 函式庫的支援

相關推薦:

PHP實作產生圖片縮圖函式

php產生圖片縮圖功能

php如何產生持透明png的圖片縮圖

##

以上是PHP基於GD庫實作產生圖片縮圖函數的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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