php生成縮略圖的兩種方法
php語言1.98W
在php中如果要生成縮略圖有什麼辦法呢?以下是小編整理的兩種方法,希望對大家有用,更多消息請關注應屆畢業生網。 核心代碼如下 public function getImage(){ // create instance $basePath = base_path('public/'); $md5 = 'b8c3bf0b10a81c5b6a98c527868a0000'; $sub1 = substr($md5, 0, 1); $sub2 = substr($md5, 1, 2); $subPath = sprintf("image/%s/%s/%s%s.jpg", $sub1, $sub2, $md5, '300x300'); $sourcePath = sprintf("image/%s/%s/%s.jpg", $sub1, $sub2, $md5); //截成100x80的縮略圖 //$image = $this->getCropper($basePath.$sourcePath,$basePath.$subPath, 300, 300); $image = $this->getCrops($basePath.$sourcePath,$basePath.$subPath, 300, 300); } //生成縮略圖 function getCropper($source_path,$NewImagePath, $target_width, $target_height) { $source_info = getimagesize($source_path); $source_width = $source_info[0]; $source_height = $source_info[1]; $source_mime = $source_info['mime']; $source_ratio = $source_height / $source_width; $target_ratio = $target_height / $target_width; // 源圖過高 if ($source_ratio > $target_ratio) { $cropped_width = $source_width; $cropped_height = $source_width * $target_ratio; $source_x = 0; $source_y = ($source_height - $cropped_height) / 2; } // 源圖過寬 elseif ($source_ratio < $target_ratio) { $cropped_width = $source_height / $target_ratio; $cropped_height = $source_height; $source_x = ($source_width - $cropped_width) / 2; $source_y = 0; } // 源圖適中 else { $cropped_width = $source_width; $cropped_height = $source_height; $source_x = 0; $source_y = 0; } switch ($source_mime) { case 'image/gif': $source_image = imagecreatefromgif($source_path); break; case 'image/jpeg': $source_image = imagecreatefromjpeg($source_path); break; case 'image/png': $source_image = imagecreatefrompng($source_path); break; default: return false; break; } $target_image = imagecreatetruecolor($target_width, $target_height); $cropped_image = imagecreatetruecolor($cropped_width, $cropped_height); // 圖片裁剪 imagecopy($cropped_image, $source_image, 0, 0, $source_x, $source_y, $cropped_width, $cropped_height); // 圖片縮放 imagecopyresampled($target_image, $cropped_image, 0, 0, 0, 0, $target_width, $target_height, $cropped_width, $cropped_height); header('Content-Type: image/jpeg'); imagejpeg($target_image,$NewImagePath,100); imagedestroy($source_image); imagedestroy($target_image); imagedestroy($cropped_image); } //生成縮略圖,填充白邊 function getCrops($src_path,$NewImagePath,$width,$height){ //源圖對象 $src_image = imagecreatefromstring(file_get_contents($src_path)); $source_info = getimagesize($src_path); $source_mime = $source_info['mime']; $src_width = imagesx($src_image); $src_height = imagesy($src_image); switch ($source_mime) { case 'image/gif': $src_image = imagecreatefromgif($src_path); break; case 'image/jpeg': $src_image = imagecreatefromjpeg($src_path); break; case 'image/png': $src_image = imagecreatefrompng($src_path); break; default: return false; break; } //生成等比例的縮略圖 //$tmp_image_width = 0; //$tmp_image_height = 0; if ($src_width / $src_height >= $width / $height) { $tmp_image_width = $width; $tmp_image_height = round($tmp_image_width * $src_height / $src_width); } else { $tmp_image_height = $height; $tmp_image_width = round($tmp_image_height * $src_width / $src_height); } $tmpImage = imagecreatetruecolor($tmp_image_width, $tmp_image_height); imagecopyresampled($tmpImage, $src_image, 0, 0, 0, 0, $tmp_image_width, $tmp_image_height, $src_width, $src_height); //添加白邊 $final_image = imagecreatetruecolor($width, $height); $color = imagecolorallocate($final_image, 255, 255, 255); imagefill($final_image, 0, 0, $color); $x = round(($width - $tmp_image_width) / 2); $y = round(($height - $tmp_image_height) / 2); imagecopy($final_image, $tmpImage, $x, $y, 0, 0, $tmp_image_width, $tmp_image_height); //輸出圖片 header('Content-Type: image/jpeg'); imagejpeg($final_image,$NewImagePath,100); imagedestroy($src_image); imagedestroy($final_image); }
-
零基礎學習PHP應該怎麼學
在網上可以看到很多學習php語言的方法,鮮有適合零基礎自學的,當然你要有其他主流語言的從業經驗就另説了,比如你從事java開發3年,python3年之類的,並且英語已經過了6級,閲讀英文書籍和文檔沒什麼壓力,可以按照那些方式學一下。但是隻是感興趣想要學習php語言,不如看看...
-
PHP如何遞歸實現json類
PHP如何遞歸實現json類呢?下面是小編給大家提供的實現方法,大家可以參考閲讀,更多詳情請關注應屆畢業生考試網。代碼如下:<?php/**@anthor:QD*@time:2013-09-27*/classjson{private$Arr=array();//傳入數組//構造器publicfunctionjson($array){if(!is_array($arr...
-
提高PHP執行效率的50個技巧
PHP是一種HTML內嵌式的語言,是一種在服務器端執行的嵌入HTML文檔的腳本語言,下面是小編為大家整理的提高PHP執行效率的50個技巧,歡迎參考~1、用單引號代替雙引號來包含字符串,這樣做會更快一些。因為PHP會在雙引號包圍的字符串中搜尋變量,單引號則不會,注意:只有echo...
-
php新手易犯的幾個小錯誤
本文為大家列舉了php新手易犯的幾個小錯誤,供大家參考。1、headeralreadysent這個錯誤通常會在你使用HEADER的時候出現,他可能是幾種原因。跟隨小編去看看吧!1、headeralreadysent這個錯誤通常會在你使用HEADER的時候出現,他可能是幾種原因:1,你在使用HEADER前PRING...