最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

PHP生成縮略圖實例講解

 更新時間:2021年03月05日 16:44:55   作者:祝君圓夢  
這篇文章主要介紹了PHP生成縮略圖實例講解,文章列舉了實例代碼,有正好需要的同學可以借鑒下

封裝的方法函數(shù):

<?php
	/**
  * 生成縮略圖
  * $imgSrc     圖片源路徑
  * $thumbWidth   縮略圖寬度
  * $thumbHeight  縮略圖高度
  * $thumbSrc    縮略圖路徑
  * $isCut     是否剪切圖片
  */
  function createThumbImg($imgSrc, $thumbWidth, $thumbHeight, $thumbSrc, $isCut = false) {
    //1.獲取圖片的類型
    $type = substr(strrchr($imgSrc, "."), 1);
    //2.初始化圖象
    if ($type == "jpg" || $type == "jpeg") {
			//創(chuàng)建一塊畫布,并從JPEG文件或URL地址載入一副圖像
      $sourceImg = imagecreatefromjpeg($imgSrc);
    }elseif ($type == "gif") {
			//創(chuàng)建一塊畫布,并從GIF文件或URL地址載入一副圖像
      $sourceImg = imagecreatefromgif($imgSrc);
    }elseif ($type == "png") {
			//創(chuàng)建一塊畫布,并從PNG文件或URL地址載入一副圖像
      $sourceImg = imagecreatefrompng($imgSrc);
    }
		elseif ($type == "wbmp") {
			//創(chuàng)建一塊畫布,并從WBMP文件或URL地址載入一副圖像
      $sourceImg = imagecreatefromwbmp($imgSrc);
    }
		//取得圖像寬度
    $width = imagesx($sourceImg);
		//取得圖像高度
    $height = imagesy($sourceImg);
 
    //3.生成圖象
    //縮略圖的圖象比例
    $scale = ($thumbWidth) / ($thumbHeight);
    //源圖片的圖象比例
    $ratio = ($width) / ($height);
    if (($isCut) == 1) {
			//高度優(yōu)先
      if ($ratio >= $scale) {        
				//創(chuàng)建真彩圖像資源(imagecreatetruecolor()函數(shù)使用GDLibrary創(chuàng)建新的真彩色圖像)
        $newimg = imagecreatetruecolor($thumbWidth, $thumbHeight);
				//圖像處理
        imagecopyresampled($newimg, $sourceImg, 0, 0, 0, 0, $thumbWidth, $thumbHeight, (($height) * $scale), $height);
        //以JPEG格式將圖像輸出到瀏覽器或文件
				ImageJpeg($newimg, $thumbSrc);
      }
			 //寬度優(yōu)先
      if ($ratio < $scale) {       
        $newimg = imagecreatetruecolor($thumbWidth, $thumbHeight);
        imagecopyresampled($newimg, $sourceImg, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $width, (($width) / $scale));
        ImageJpeg($newimg, $thumbSrc);
      }
    } else {
      if ($ratio >= $scale) {
        $newimg = imagecreatetruecolor($thumbWidth, ($thumbWidth) / $ratio);
        imagecopyresampled($newimg, $sourceImg, 0, 0, 0, 0, $thumbWidth, ($thumbWidth) / $ratio, $width, $height);
        ImageJpeg($newimg, $thumbSrc);
      }
      if ($ratio < $scale) {
        $newimg = imagecreatetruecolor(($thumbHeight) * $ratio, $thumbHeight);
        imagecopyresampled($newimg, $sourceImg, 0, 0, 0, 0, ($thumbHeight) * $ratio, $thumbHeight, $width, $height);
        ImageJpeg($newimg, $thumbSrc);
      }
    }
		//銷毀圖像
    ImageDestroy($sourceImg);
  }
?>

調用示例:

<?php
	//圖片源路徑
	$imgSrc="D:/PHP/test/demo.jpg";
	//縮略圖路徑
	$thumbSrc="D:/PHP/test/thumb.jpg";
	createThumbImg($path,100,100,$thumbSrc);
?>

到此這篇關于PHP生成縮略圖實例講解的文章就介紹到這了,更多相關PHP生成縮略圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論

临漳县| 南靖县| 拜泉县| 临洮县| 苏州市| 崇义县| 江北区| 长寿区| 云阳县| 娄底市| 通许县| 万年县| 云和县| 华安县| 包头市| 洛隆县| 乐昌市| 泰顺县| 梅河口市| 栾川县| 鸡东县| 综艺| 佛坪县| 盐源县| 衡东县| 丰镇市| 江孜县| 安图县| 璧山县| 浦东新区| 太保市| 黄梅县| 葵青区| 淳化县| 滕州市| 北川| 雅安市| 股票| 大冶市| 合作市| 石城县|