解決php剪切縮略圖生成png,PHP縮略圖

本文目錄一覽:

PHP 下載圖片轉換格式的問題?

你需要 PHP 的 GD 擴展組件來轉換 png/gif 到 jpg。

注意 jpg 會忽略 alpha(透明度),下面的函數將背景默認為白色,壓縮設置為 80%。

函數有兩個參數:$file = 要轉換的 png/gif 文件,$jpg = 輸出的 jpg 文件。

?php

function img2jpg($file, $jpg) {

$ext = pathinfo($file, PATHINFO_EXTENSION);

if($ext == “png”)

$image = imagecreatefrompng($file);

else if($ext == “gif”)

$image = imagecreatefromgif($file);

else

return true;

$bg = imagecreatetruecolor(imagesx($image), imagesy($image));

imagefill($bg, 0, 0, imagecolorallocate($bg, 255, 255, 255));

imagecopy($bg, $image, 0, 0, 0, 0, imagesx($image), imagesy($image));

imagedestroy($image);

imagejpeg($bg, $jpg, 80);

ImageDestroy($bg);

}

img2jpg(“image.png”, “image.jpg”);

?

如果你運行上面的代碼後出現 Call to undefined function imagecreatefrompng() 類似的錯誤,那應該是 PHP 沒有開啟 GD 擴展組件。

php創建縮略圖問題

其實PHP創建縮略圖就是在PHP在原圖片的基礎上創建一張新的圖片的過程,而用PHP創建圖像的過程一般分成四部:

第一步:創建一張畫布(只要是畫圖都需要一張畫布的)

第二步:在畫布畫東西(可以畫各種圖形,如長方形,直線,等等,也可以在畫布上寫字啥的,或者畫其他的圖形)

第三步:畫完圖之後,將圖片輸出,將圖片輸出到瀏覽器,在瀏覽器顯示出來,或者保存為一張新 的圖片(縮略圖一般是保存為圖片文件的)

第四步:因為創建畫布時打開了文件流,所以要關閉資源,節省內存。(個人覺得你可以這樣理解,打開了一畫布,把它鋪開了,畫完了就把畫布捲起來,收起來,不要佔著鋪的地方)

具體的代碼如下:(這段代碼來源於ThinkPHP的圖像類)

?php

class Thumb{

   /**

     * @param string $image  原圖

     * @param string $thumbname 縮略圖文件名

     * @param string $type 圖像格式

     * @param string $maxWidth  寬度

     * @param string $maxHeight  高度

   */

   static create($img, $thumbname, $type=”, $maxWidth=200, $maxHeight=50)

   {

       $info = getimagesize($img);    //獲取原圖的圖像信息(長、寬、格式等)

       if ($info !== false) {

            $srcWidth = $info[‘width’];

            $srcHeight = $info[‘height’];

            $type = empty($type) ? $info[‘type’] : $type;

            $type = strtolower($type);

            $interlace = $interlace ? 1 : 0;

            unset($info);

            $scale = min($maxWidth / $srcWidth, $maxHeight / $srcHeight); // 計算縮放比例

            if ($scale = 1) {

                // 超過原圖大小不再縮略

                $width = $srcWidth;

                $height = $srcHeight;

            } else {

                // 縮略圖尺寸

                $width = (int) ($srcWidth * $scale);

                $height = (int) ($srcHeight * $scale);

            }

            // 載入原圖(在原圖的基礎上創建畫布,為第一步)

            $createFun = ‘ImageCreateFrom’ . ($type == ‘jpg’ ? ‘jpeg’ : $type);

            if(!function_exists($createFun)) {

                return false;

            }

            $srcImg = $createFun($image);

            //第二步開始

            //創建縮略圖

            if ($type != ‘gif’  function_exists(‘imagecreatetruecolor’))

                $thumbImg = imagecreatetruecolor($width, $height);

            else

                $thumbImg = imagecreate($width, $height);

              //png和gif的透明處理 by luofei614

            if(‘png’==$type){

                imagealphablending($thumbImg, false);//取消默認的混色模式(為解決陰影為綠色的問題)

                imagesavealpha($thumbImg,true);//設定保存完整的 alpha 通道信息(為解決陰影為綠色的問題)    

            }elseif(‘gif’==$type){

                $trnprt_indx = imagecolortransparent($srcImg);

                 if ($trnprt_indx = 0) {

                        //its transparent

                       $trnprt_color = imagecolorsforindex($srcImg , $trnprt_indx);

                       $trnprt_indx = imagecolorallocate($thumbImg, $trnprt_color[‘red’], $trnprt_color[‘green’], $trnprt_color[‘blue’]);

                       imagefill($thumbImg, 0, 0, $trnprt_indx);

                       imagecolortransparent($thumbImg, $trnprt_indx);

              }

            }

            // 複製圖片

            if (function_exists(“ImageCopyResampled”))

                imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight);

            else

                imagecopyresized($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight);

           //第三步:輸出圖像

            // 生成圖片

            $imageFun = ‘image’ . ($type == ‘jpg’ ? ‘jpeg’ : $type);

            $imageFun($thumbImg, $thumbname);

            

            //第四步:關閉畫布

            imagedestroy($thumbImg);

            imagedestroy($srcImg);

            return $thumbname;

        }

        return false;

       

   }

}

?

你使用的時候直接用:

require Thumb.class.php

$thumb = Thumb::create(‘s.jpg’,’thumb_s.jpg’,100,50);

希望我的回答你能滿意

誰有php批量處理圖片、圖片生成縮略圖、圖片添加水印的函數?

//批量處理圖片、圖片生成縮略圖、圖片添加水印

$dir = opendir (dirname(__FILE__));

while (!!$_file = readdir($dir)){

 list($filesname,$kzm)=explode(“.”,$_file);//獲取擴展名

 if($kzm==”gif” or $kzm==”jpg” or $kzm==”JPG” or $kzm==”png”) {

  if(!makethumb(“$_file”,”120″,”120″,”100″)){

   echo ‘執行成功!’;

  }else{

   echo ‘執行失敗!’;

  }

 }

}

closedir($dir);

/**

 * 處理縮略圖並添加水印函數

 * @access publiuc

 * @param $srcFile———–圖片文件名

 * @param $dstFile———–另存的文件名

 * @param $dstW————-圖片保存的寬度

 * @param $dstH————–圖片保存的高度

 * @param $rate—————圖片保存的品質

 * @param $markwords—–水印文字

 * @param $markimage—–水印圖片

 * @param 使用方法 makethumb(“a.jpg”,”b.jpg”,”120″,”120″,”100″);

 */

function makethumb($srcFile/*,$dstFile*/,$dstW,$dstH,$rate=100/*,$markwords=null,$markimage=null*/) {

 

 $data = GetImageSize($srcFile);

 switch($data[2]) {

  case 1:

  $im=@ImageCreateFromGIF($srcFile);

  break;

  case 2:

  $im=@ImageCreateFromJPEG($srcFile);

  break;

  case 3:

  $im=@ImageCreateFromPNG($srcFile);

  break;

  }

 if(!$im) return False;

 $srcW=ImageSX($im);

 $srcH=ImageSY($im);

 $dstX=0;

 $dstY=0;

 if ($srcW*$dstH$srcH*$dstW) {

 $fdstH = round($srcH*$dstW/$srcW);

 $dstY = floor(($dstH-$fdstH)/2);

 $fdstW = $dstW;

 }

 else

 {

 $fdstW = round($srcW*$dstH/$srcH);

 $dstX = floor(($dstW-$fdstW)/2);

 $fdstH = $dstH;

 }

$ni=ImageCreateTrueColor($dstW,$dstH);

$dstX=($dstX0)?0:$dstX;

$dstY=($dstX0)?0:$dstY;

$dstX=($dstX($dstW/2))?floor($dstW/2):$dstX;

$dstY=($dstY($dstH/2))?floor($dstH/s):$dstY;

$white = ImageColorAllocate($ni,255,255,255);

$black = ImageColorAllocate($ni,0,0,0);

imagefilledrectangle($ni,0,0,$dstW,$dstH,$white);// 填充背景色

ImageCopyResized($ni,$im,$dstX,$dstY,0,0,$fdstW,$fdstH,$srcW,$srcH);

//if($markwords!=null){

// $markwords=iconv(“gb2312″,”UTF-8”,$markwords);

// //轉換文字編碼

// ImageTTFText($ni,20,30,450,560,$black,”simhei.ttf”,$markwords); //寫入文字水印,參數依次為,文字大小|偏轉度|橫坐標|縱坐標|文字顏色|文字類型|文字內容

//}elseif($markimage!=null) {

// $wimage_data = GetImageSize($markimage);

// switch($wimage_data[2]) {

// case 1:

//  $wimage=@ImageCreateFromGIF($markimage);

//  break;

// case 2:

//  $wimage=@ImageCreateFromJPEG($markimage);

//  break;

// case 3:

//  $wimage=@ImageCreateFromPNG($markimage);

//  break;

// }

// imagecopy($ni,$wimage,500,560,0,0,88,31); //寫入圖片水印,水印圖片大小默認為88*31

// imagedestroy($wimage);

//}

$dstFile = $srcFile.’.gif’;

ImageJpeg($ni,$dstFile,$rate);

//ImageJpeg($ni,$srcFile,$rate);

imagedestroy($im);

imagedestroy($ni);

}

php怎麼生成縮略圖

給你個函數吧

 // *****生成縮略圖*****

     // 只考慮jpg,png,gif格式

     // $srcImgPath 源圖象路徑

     // $targetImgPath 目標圖象路徑

     // $targetW 目標圖象寬度

     // $targetH 目標圖象高度

     function makeThumbnail($srcImgPath,$targetImgPath,$targetW,$targetH)

     {

         $imgSize = GetImageSize($srcImgPath);

         $imgType = $imgSize[2];

         //@ 使函數不向頁面輸出錯誤信息

         switch ($imgType)

        {

            case 1:

                $srcImg = @ImageCreateFromGIF($srcImgPath);

                break;

            case 2:

                $srcImg = @ImageCreateFromJpeg($srcImgPath);

                break;

            case 3:

                $srcImg = @ImageCreateFromPNG($srcImgPath);

                break;

        }

         //取源圖象的寬高

        $srcW = ImageSX($srcImg);

        $srcH = ImageSY($srcImg);

        if($srcW$targetW || $srcH$targetH)

        {

            $targetX = 0;

            $targetY = 0;

            if ($srcW  $srcH)

            {

                $finaW=$targetW;

                $finalH=round($srcH*$finaW/$srcW);

                $targetY=floor(($targetH-$finalH)/2);

            }

            else

            {

                $finalH=$targetH;

                $finaW=round($srcW*$finalH/$srcH);

                $targetX=floor(($targetW-$finaW)/2);

            }

              //function_exists 檢查函數是否已定義

              //ImageCreateTrueColor 本函數需要GD2.0.1或更高版本

            if(function_exists(“ImageCreateTrueColor”))

            {

                $targetImg=ImageCreateTrueColor($targetW,$targetH);

            }

            else

              {

                $targetImg=ImageCreate($targetW,$targetH);

            }

            $targetX=($targetX0)?0:$targetX;

            $targetY=($targetX0)?0:$targetY;

            $targetX=($targetX($targetW/2))?floor($targetW/2):$targetX;

            $targetY=($targetY($targetH/2))?floor($targetH/2):$targetY;

              //背景白色

            $white = ImageColorAllocate($targetImg, 255,255,255);

            ImageFilledRectangle($targetImg,0,0,$targetW,$targetH,$white);

            /*

                   PHP的GD擴展提供了兩個函數來縮放圖象:

                   ImageCopyResized 在所有GD版本中有效,其縮放圖象的演算法比較粗糙,可能會導致圖象邊緣的鋸齒。

                   ImageCopyResampled 需要GD2.0.1或更高版本,其像素插值演算法得到的圖象邊緣比較平滑,

                                                             該函數的速度比ImageCopyResized慢。

            */

            if(function_exists(“ImageCopyResampled”))

            {

                ImageCopyResampled($targetImg,$srcImg,$targetX,$targetY,0,0,$finaW,$finalH,$srcW,$srcH);

            }

            else

            {

                ImageCopyResized($targetImg,$srcImg,$targetX,$targetY,0,0,$finaW,$finalH,$srcW,$srcH);

            }

              switch ($imgType) {

                case 1:

                    ImageGIF($targetImg,$targetImgPath);

                    break;

                case 2:

                    ImageJpeg($targetImg,$targetImgPath);

                    break;

                case 3:

                    ImagePNG($targetImg,$targetImgPath);

                    break;

            }

            ImageDestroy($srcImg);

            ImageDestroy($targetImg);

        }

         else //不超出指定寬高則直接複製

        {

            copy($srcImgPath,$targetImgPath);

            ImageDestroy($srcImg);

        }

     }

代碼已經測試,成功運行!

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/249825.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-12 17:12
下一篇 2024-12-12 17:12

相關推薦

  • PHP和Python哪個好找工作?

    PHP和Python都是非常流行的編程語言,它們被廣泛應用於不同領域的開發中。但是,在考慮擇業方向的時候,很多人都會有一個問題:PHP和Python哪個好找工作?這篇文章將從多個方…

    編程 2025-04-29
  • PHP怎麼接幣

    想要在自己的網站或應用中接受比特幣等加密貨幣的支付,就需要對該加密貨幣擁有一定的了解,並使用對應的API進行開發。本文將從多個方面詳細闡述如何使用PHP接受加密貨幣的支付。 一、環…

    編程 2025-04-29
  • 使用PHP foreach遍歷有相同屬性的值

    本篇文章將介紹如何使用PHP foreach遍歷具有相同屬性的值,並給出相應的代碼示例。 一、基礎概念 在講解如何使用PHP foreach遍歷有相同屬性的值之前,我們需要先了解幾…

    編程 2025-04-28
  • PHP獲取301跳轉後的地址

    本文將為大家介紹如何使用PHP獲取301跳轉後的地址。301重定向是什麼呢?當我們訪問一個網頁A,但是它已經被遷移到了另一個地址B,此時若伺服器端做了301重定向,那麼你的瀏覽器在…

    編程 2025-04-27
  • PHP登錄頁面代碼實現

    本文將從多個方面詳細闡述如何使用PHP編寫一個簡單的登錄頁面。 1. PHP登錄頁面基本架構 在PHP登錄頁面中,需要包含HTML表單,用戶在表單中輸入賬號密碼等信息,提交表單後服…

    編程 2025-04-27
  • PHP與Python的比較

    本文將會對PHP與Python進行比較和對比分析,包括語法特性、優缺點等方面。幫助讀者更好地理解和使用這兩種語言。 一、語法特性 PHP語法特性: <?php // 簡單的P…

    編程 2025-04-27
  • PHP版本管理工具phpenv詳解

    在PHP項目開發過程中,我們可能需要用到不同版本的PHP環境來試驗不同的功能或避免不同版本的兼容性問題。或者我們需要在同一台伺服器上同時運行多個不同版本的PHP語言。但是每次手動安…

    編程 2025-04-24
  • PHP數組去重詳解

    一、array_unique函數 array_unique是php中常用的數組去重函數,它基於值來判斷元素是否重複,具體使用方法如下: $array = array(‘a’, ‘b…

    編程 2025-04-24
  • PHP導出Excel文件

    一、PHP導出Excel文件列寬調整 當我們使用PHP導出Excel文件時,有時需要調整單元格的列寬。可以使用PHPExcel類庫中的setWidth方法來設置單元格的列寬。下面是…

    編程 2025-04-24
  • php擴展庫初探

    一、什麼是php擴展庫? PHP擴展庫(PHP extension)是一些用C語言編寫的動態鏈接庫,用於擴展PHP的功能。PHP擴展庫使得PHP可以與各種資料庫系統相連、SMTP、…

    編程 2025-04-23

發表回復

登錄後才能評論