php生成縮略圖的兩種方法(代碼)
PHP是最近的熱門語言,那么如果php要生成縮略圖有什么辦法呢?其實php生成縮略圖有兩種方法,核心代碼如下,更多消息請關注應屆畢業(yè)生網(wǎng)!
public function getImage(){
/pic/div>
$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);/pic/pic/div>
/pic/div>
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;/pic/div>
{
$cropped_width = $source_width;
$cropped_height = $source_width * $target_ratio;$source_x = 0;$source_y = ($source_height - $cropped_height) / 2;}
/pic/div>
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;}
/pic/div>
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);/pic/pic/jpeg');imagejpeg($target_image,$NewImagePath,100);imagedestroy($source_image);imagedestroy($target_image);
imagedestroy($cropped_image);
}
/pic/div>
function getCrops($src_path,$NewImagePath,$width,$height){/pic/div>
$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;
}
/pic/div>
/pic/div>
/pic/div>
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);/pic/ 2);$y = round(($height - $tmp_image_height) / 2);imagecopy($final_image, $tmpImage, $x, $y, 0, 0, $tmp_image_width, $tmp_image_height);/pic/jpeg');
imagejpeg($final_image,$NewImagePath,100);imagedestroy($src_image);imagedestroy($final_image);
}
【php生成縮略圖的兩種方法代碼】相關文章:
php生成縮略圖的兩種方法02-12
PHP生成縮略圖的方法03-17
PHP生成圖片縮略圖類示例代碼03-08
php生成圖片縮略圖的方法07-25
PHP生成縮略圖的類的方法08-05
PHP生成SiteMap文件代碼的方法03-01
php生成高清縮略圖實例02-05
php上傳圖片生成縮略圖02-17