av手机免费在线观看,国产女人在线视频,国产xxxx免费,捆绑调教一二三区,97影院最新理论片,色之久久综合,国产精品日韩欧美一区二区三区

php語言

php生成高清縮略圖實例

時間:2025-02-05 11:06:51 php語言 我要投稿
  • 相關(guān)推薦

php生成高清縮略圖實例

  php生成高清縮略圖的方法,較為詳細的分析了php生成縮略圖時出現(xiàn)失真的解決方法,并給出了完整實例進行總結(jié)分析,需要的朋友可以參考下。

  在使用php的函數(shù)生成縮略圖的使用,縮略圖很多情況下都會失真,這個時候需要有一些對應(yīng)的解決方法。

  1.用imagecreatetruecolor和imageCopyreSampled函數(shù)分別取代imagecreate和imagecopyresized

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

  下面是具體的函數(shù)

  function CreateSmallImage( $OldImagePath, $NewImagePath, $NewWidth=154, $NewHeight=134)

  {

  // 取出原圖,獲得圖形信息getimagesize參數(shù)說明: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);

  // 創(chuàng)建圖形,imagecreate參數(shù)說明:寬,高

  $NewImg = imagecreatetruecolor( $NewWidth, $NewHeight );

  //創(chuàng)建色彩,參數(shù):圖形,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]); //要寫入的寬度

  //這樣處理圖片比例會失調(diào),但可以填滿背景

  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為基礎(chǔ),如果新寬小于或等于$NewWidth,則成立

  if ( $WriteNewWidth <= $NewWidth ) {

  $WriteNewWidth = $WriteNewWidth; //用判斷后的大小

  $WriteNewHeight = $NewHeight; //用規(guī)定的大小

  $WriteX = floor( ($NewWidth-$WriteNewWidth) / 2 ); //在新圖片上寫入的X位置計算

  $WriteY = 0;

  } else {

  $WriteNewWidth = $NewWidth; // 用規(guī)定的大小

  $WriteNewHeight = $WriteNewHeight; //用判斷后的大小

  $WriteX = 0;

  $WriteY = floor( ($NewHeight-$WriteNewHeight) / 2 ); //在新圖片上寫入的X位置計算

  }

  //舊圖形縮小后,寫入到新圖形上(復(fù)制),imagecopyresized參數(shù)說明:新舊, 新xy舊xy, 新寬高舊寬高

  @imageCopyreSampled( $NewImg, $OldImg, $WriteX, $WriteY, 0, 0, $WriteNewWidth, $WriteNewHeight, $OldImageInfo[0], $OldImageInfo[1] );

  //保存文件

  //  @imagegif( $NewImg, $NewImagePath );

  @imagejpeg($NewImg, $NewImagePath, 100);

  //結(jié)束圖形

  @imagedestroy($NewImg);

  }

  CreateSmallImage("./images/jiexie.jpg","./images/jiexie.small.jpg",200,300);

  CreateSmallImage("./images/jiexie.jpg","./images/jiexie.middle.jpg",400,500);

  希望本文所述對大家php程序設(shè)計有所幫助。

【php生成高清縮略圖實例】相關(guān)文章:

PHP生成縮略圖的方法03-17

php上傳圖片生成縮略圖07-31

PHP生成縮略圖的類的方法07-11

php生成圖片縮略圖的方法06-06

php生成圖片縮略圖功能示例05-28

PHP生成器簡單實例04-13

PHP生成圖片縮略圖類示例代碼07-31

php生成縮略圖的兩種方法05-24

php生成sitemap.xml的實例代碼06-02