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

PHP生成縮略圖實(shí)例講解

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

封裝的方法函數(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);
  }
?>

調(diào)用示例:

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

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

相關(guān)文章

最新評(píng)論

阿拉善左旗| 安丘市| 岢岚县| 卫辉市| 北宁市| 徐汇区| 秦安县| 越西县| 龙井市| 赤峰市| 西充县| 钟山县| 郓城县| 巴彦淖尔市| 竹山县| 集安市| 株洲县| 台南市| 安化县| 临泽县| 阿拉善右旗| 册亨县| 晋江市| 镇雄县| 寻甸| 沙洋县| 乌鲁木齐县| 长春市| 西林县| 吴忠市| 乐亭县| 武宁县| 谷城县| 遂平县| 金乡县| 文昌市| 博罗县| 梓潼县| 雷州市| 汤阴县| 泗水县|