php產生高清縮圖執行個體詳解_php技巧

來源:互聯網
上載者:User

本文執行個體講述了php產生高清縮圖的方法。分享給大家供大家參考,具體如下:

在使用php的函數產生縮圖的使用,縮圖很多情況下都會失真,這個時候需要有一些對應的解決方案

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

2.給imagejpeg的第三個參數帶上100(例: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程式設計有所協助。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.