本文目錄一覽:
PHP如何把上傳的照片生成高質量的縮略圖?
ImageMagick沒用過,一般直接用內置的GD庫,沒有發現你說的這麼嚴重的失真問題。
利用GD庫創建縮略圖的大致思路如下:
依據設定的尺寸創建真彩色畫布$im=createtruecolor(120,90);
讀取原始文件尺寸,按照原始尺寸的寬度和高度比例,計算出縮略圖的大小(可能與給定的尺寸有一定的偏差)
將原始圖像拷貝並縮放到創建的真彩色縮略圖畫布上。
輸出縮略圖文件。
可能就是因為利用的是這個真彩色,縮略圖效果還湊合,也不是說絕對不失真的。
PHP 圖片上傳生成縮略圖
//2014年3月5日15:08:02 因為需要做縮略圖,所以改用thinkphp來做上傳,它支持時間戳命名,方便命名,以及更名
//這是以前百度到的,然後使用的縮略圖代碼,需要cg庫支持
/**
* 生成縮略圖
* @author yangzhiguo0903@163.com
* @param string 源圖絕對完整地址{帶文件名及後綴名}
* @param string 目標圖絕對完整地址{帶文件名及後綴名}
* @param int 縮略圖寬{0:此時目標高度不能為0,目標寬度為源圖寬*(目標高度/源圖高)}
* @param int 縮略圖高{0:此時目標寬度不能為0,目標高度為源圖高*(目標寬度/源圖寬)}
* @param int 是否裁切{寬,高必須非0}
* @param int/float 縮放{0:不縮放, 0this1:縮放到相應比例(此時寬高限制和裁切均失效)}
* @return boolean
*/
function fileext($file)
{
return strtolower(pathinfo($file, PATHINFO_EXTENSION));
}
function img2thumb($src_img, $dst_img, $width = 75, $height = 75, $cut = 0, $proportion = 0)
{
if(!is_file($src_img))
{
return false;
}
$ot = $this-fileext($dst_img);
$otfunc = ‘image’ . ($ot == ‘jpg’ ? ‘jpeg’ : $ot);
$srcinfo = getimagesize($src_img);
$src_w = $srcinfo[0];
$src_h = $srcinfo[1];
$type = strtolower(substr(image_type_to_extension($srcinfo[2]), 1));
$createfun = ‘imagecreatefrom’ . ($type == ‘jpg’ ? ‘jpeg’ : $type);
$dst_h = $height;
$dst_w = $width;
$x = $y = 0;
/**
* 縮略圖不超過源圖尺寸(前提是寬或高只有一個)
*/
if(($width $src_w $height $src_h) || ($height $src_h $width == 0) || ($width $src_w $height == 0))
{
$proportion = 1;
}
if($width $src_w)
{
$dst_w = $width = $src_w;
}
if($height $src_h)
{
$dst_h = $height = $src_h;
}
if(!$width !$height !$proportion)
{
return false;
}
if(!$proportion)
{
if($cut == 0)
{
if($dst_w $dst_h)
{
if($dst_w/$src_w $dst_h/$src_h)
{
$dst_w = $src_w * ($dst_h / $src_h);
$x = 0 – ($dst_w – $width) / 2;
}
else
{
$dst_h = $src_h * ($dst_w / $src_w);
$y = 0 – ($dst_h – $height) / 2;
}
}
else if($dst_w xor $dst_h)
{
if($dst_w !$dst_h) //有寬無高
{
$propor = $dst_w / $src_w;
$height = $dst_h = $src_h * $propor;
}
else if(!$dst_w $dst_h) //有高無寬
{
$propor = $dst_h / $src_h;
$width = $dst_w = $src_w * $propor;
}
}
}
else
{
if(!$dst_h) //裁剪時無高
{
$height = $dst_h = $dst_w;
}
if(!$dst_w) //裁剪時無寬
{
$width = $dst_w = $dst_h;
}
$propor = min(max($dst_w / $src_w, $dst_h / $src_h), 1);
$dst_w = (int)round($src_w * $propor);
$dst_h = (int)round($src_h * $propor);
$x = ($width – $dst_w) / 2;
$y = ($height – $dst_h) / 2;
}
}
else
{
$proportion = min($proportion, 1);
$height = $dst_h = $src_h * $proportion;
$width = $dst_w = $src_w * $proportion;
}
$src = $createfun($src_img);
$dst = imagecreatetruecolor($width ? $width : $dst_w, $height ? $height : $dst_h);
$white = imagecolorallocate($dst, 255, 255, 255);
imagefill($dst, 0, 0, $white);
if(function_exists(‘imagecopyresampled’))
{
imagecopyresampled($dst, $src, $x, $y, 0, 0, $dst_w, $dst_h, $src_w, $src_h);
}
else
{
imagecopyresized($dst, $src, $x, $y, 0, 0, $dst_w, $dst_h, $src_w, $src_h);
}
$otfunc($dst, $dst_img);
imagedestroy($dst);
imagedestroy($src);
return true;
}
php 上傳圖片 並生成縮略圖
一般上傳圖片會返回一個上傳圖片的文件名,上傳會指定你的上傳路徑。例如:www/project/Uploads/Images/,用設個路徑+文件名,然後傳入makethumb就ok了,樓主可以去網上看一看thinkPHP的文件上傳類和圖像處理類,官方也有教程的。
php上傳圖片自動生成縮略圖
########################## index.php
form action=”index.php?item=file_upload_ok” method=”post” enctype=”multipart/form-data”
input type=”file” name=”download”
INPUT TYPE=”text” NAME=”describe”
INPUT TYPE=”submit” value=”提交”
/form
?
if(!empty($_GET[‘item’])){
if($_GET[‘item’] == ‘file_upload_ok’)
{
// 引入圖片類
include(“thumb_class.php”);
$t = new ThumbHandler();
// 獲取上傳文件
$file=$_FILES[download];
$yName = $file[name]; // 原文件名字
$tmpName = $file[tmp_name]; // 原文件句柄
// 圖片 縮放
$t-setSrcImg($tmpName); //原文件
$t-setDstImg(“new_x.jpg”);//目標圖片
$t-createImg(80,80); //生成圖片 寬 300 高 300
// 圖片原始大小
$t-setSrcImg($tmpName); //原文件
$t-setDstImg(“new_d.jpg”);//目標圖片
$t-createImg($t-src_w,$t-src_h); //生成圖片 $t-src_w 原圖片寬 $t-src_h 原圖片高
}
}
#############
thumb_class.php 圖片類文件 由於代碼過多請到我 空間 日 記 查看
h t t p ://user.qzone.qq.com/182887459/
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/309984.html