首頁  >  文章  >  後端開發  >  php產生高清縮圖的方法

php產生高清縮圖的方法

墨辰丷
墨辰丷原創
2018-06-05 17:27:161488瀏覽

這篇文章主要介紹php產生高清縮圖的方法,有興趣的朋友參考下,希望對大家有幫助。

1.用imagecreatetruecolor和imageCopyreSampled函數分別取代imagecreate和imagecopyresized

2.給imagejpeg的第三個參數帶上100(例:imagejpeg(例:imagejpeg(例:imagejpeg( $ni,$toFile,100))

以下是具體的函數

function CreateSmallImage( $OldImagePath, $NewImagePath, $NewWidth=154, $NewHeight=134)
{
  // 取出原图,获得图形信息getimagesize参数说明:0(宽),1(高),2(1gif/2jpg/3png),3(width="638" height="340")
  $OldImageInfo = getimagesize($OldImagePath);
  if ( $OldImageInfo[2] == 1 ) $OldImg = @imagecreatefromgif($OldImagePath);
  elseif ( $OldImageInfo[2] == 2 ) $OldImg = @imagecreatefromjpeg($OldImagePath);
  else $OldImg = @imagecreatefrompng($OldImagePath);
  // 创建图形,imagecreate参数说明:宽,高
  $NewImg = imagecreatetruecolor( $NewWidth, $NewHeight );
  //创建色彩,参数:图形,red(0-255),green(0-255),blue(0-255)
  $black = ImageColorAllocate( $NewImg, 0, 0, 0 ); //黑色
  $white = ImageColorAllocate( $NewImg, 255, 255, 255 ); //白色
  $red  = ImageColorAllocate( $NewImg, 255, 0, 0 ); //红色
  $blue = ImageColorAllocate( $NewImg, 0, 0, 255 ); //蓝色
  $other = ImageColorAllocate( $NewImg, 0, 255, 0 );
  //新图形高宽处理
  $WriteNewWidth = $NewHeight*($OldImageInfo[0] / $OldImageInfo[1]); //要写入的高度
  $WriteNewHeight = $NewWidth*($OldImageInfo[1] / $OldImageInfo[0]); //要写入的宽度
  //这样处理图片比例会失调,但可以填满背景
  if ($OldImageInfo[0] / $NewWidth > $org_info[1] / $NewHeight) {
    $WriteNewWidth = $NewWidth;
    $WriteNewHeight = $NewWidth / ($OldImageInfo[0] / $OldImageInfo[1]);
  } else {
    $WriteNewWidth = $NewHeight * ($OldImageInfo[0] / $OldImageInfo[1]);
    $WriteNewHeight = $NewHeight;
  }
  //以$NewHeight为基础,如果新宽小于或等于$NewWidth,则成立
  if ( $WriteNewWidth <= $NewWidth ) {
    $WriteNewWidth = $WriteNewWidth; //用判断后的大小
    $WriteNewHeight = $NewHeight; //用规定的大小
    $WriteX = floor( ($NewWidth-$WriteNewWidth) / 2 ); //在新图片上写入的X位置计算
    $WriteY = 0;
  } else {
    $WriteNewWidth = $NewWidth; // 用规定的大小
    $WriteNewHeight = $WriteNewHeight; //用判断后的大小
    $WriteX = 0;
    $WriteY = floor( ($NewHeight-$WriteNewHeight) / 2 ); //在新图片上写入的X位置计算
  }
  //旧图形缩小后,写入到新图形上(复制),imagecopyresized参数说明:新旧, 新xy旧xy, 新宽高旧宽高
  @imageCopyreSampled( $NewImg, $OldImg, $WriteX, $WriteY, 0, 0, $WriteNewWidth, $WriteNewHeight, $OldImageInfo[0], $OldImageInfo[1] );
  //保存文件
//  @imagegif( $NewImg, $NewImagePath );
  @imagejpeg($NewImg, $NewImagePath, 100);
  //结束图形
  @imagedestroy($NewImg);
}
CreateSmallImage("./images/jiexie.jpg","./images/jiexie.small.jpg",200,300);
CreateSmallImage("./images/jiexie.jpg","./images/jiexie.middle.jpg",400,500);

總結:以上就是這篇文章的全部內容,希望能對大家的學習有所幫助。

相關推薦:

php分頁原理分頁程式碼分頁類別製作方法實例詳解

php基於glob函數實作遍歷檔案與目錄詳解

php版微信公眾平台開發之驗證步驟實例詳解(必讀)

以上是php產生高清縮圖的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn