本文目錄一覽:
- 1、php SWFUpload 怎麼創建縮略圖並且保存到指定文件夾裡面
- 2、能直接用的PHP生成縮略圖的程序(要求簡單)
- 3、thinkphp 3.1.2生成縮略圖 image類怎麼引入
- 4、php怎麼生成縮略圖
- 5、php創建縮略圖問題
- 6、用PHP怎麼生成高質量的縮略圖
php SWFUpload 怎麼創建縮略圖並且保存到指定文件夾裡面
php /* * swfupload圖片上傳 */ if (isset($_POST[“PHPSESSID”])) { session_id($_POST[“PHPSESSID”]); } session_start(); ini_set(“html_errors”, “0”); if (!isset($_FILES[“Filedata”]) || !is_uploaded_file($_FILES[“Filedata”][“tmp_name”]) ||$_FILES[“Filedata”][“error”] != 0) { echo “錯誤:無效的上傳!”; exit(0); } // Get the image and create a thumbnail $file_types=explode(“.”,$_FILES[“Filedata”][“name”]); $file_type=$file_types[count($file_types)-1]; if(strtolower($file_type)==’gif’ ) { $img = imagecreatefromgif($_FILES[“Filedata”][“tmp_name”]); } else if(strtolower($file_type)==’png’) { $img = imagecreatefrompng($_FILES[“Filedata”][“tmp_name”]); } else if(strtolower($file_type)==’bmp’) { $img = imagecreatefromwbmp($_FILES[“Filedata”][“tmp_name”]); } else { $img = imagecreatefromjpeg($_FILES[“Filedata”][“tmp_name”]); } if (!$img) { echo “錯誤:無法創建圖像 “. $_FILES[“Filedata”][“tmp_name”]; exit(0); } $width = imageSX($img); $height = imageSY($img); if (!$width || !$height) { echo “錯誤:無效的高或高”; exit(0); } // Build the thumbnail $target_width = 100; $target_height = 100; $target_ratio = $target_width / $target_height; $img_ratio = $width / $height; if ($target_ratio $img_ratio) { $new_height = $target_height; $new_width = $img_ratio * $target_height; } else { $new_height = $target_width / $img_ratio; $new_width = $target_width; } if ($new_height $target_height) { $new_height = $target_height; } if ($new_width $target_width) { $new_height = $target_width; } $new_img = ImageCreateTrueColor(100, 100); if (!@imagefilledrectangle($new_img, 0, 0, $target_width-1, $target_height-1, 0)) { // Fill the image black echo “錯誤:不能填充新圖片”; exit(0); } if (!@imagecopyresampled($new_img, $img, ($target_width-$new_width)/2, ($target_height-$new_height)/2, 0,0, $new_width, $new_height, $width, $height)) { echo “錯誤:不能調整大小的圖像”; exit(0); } if (!isset($_SESSION[“file_info”])) { $_SESSION[“file_info”] = array(); } ob_start(); imagejpeg($new_img); $imagevariable = ob_get_contents(); ob_end_clean(); $file_id = md5($_FILES[“Filedata”][“tmp_name”] + rand()*100000); $_SESSION[“file_info”][$file_id] = $imagevariable; echo “FILEID:” . $file_id; // Return the file id to the script include(“upimg.class.php”); if(!empty($_FILES[“Filedata”]) and count(explode(“,”,$_SESSION[“upload_tem”]))5) { $folder=”upload/images/tem/”.date(“Y-m-d”); $up = new upimg(“$folder”,”$folder”); //可以寫成:$up = new upimg(); $up-autoThumb = TRUE; //可省略 $up-srcDel=TRUE; $up-thumbWidth = 550; //可省略 $up-thumbHeight = 400; //可省略 $up-maxsize=2014; //上傳文件大小單位是kb $result= $up-upload(‘Filedata’); // HTML中input /的name屬性值 $_SESSION[“upload_tem”]=$_SESSION[“upload_tem”].”,”.$up-thumbPath; $_SESSION[“upload_tem”]=trim($_SESSION[“upload_tem”],”,”); } ?2. [代碼][PHP]代碼 生成縮略圖類upimg.class.php: ?php class upimg{ public $uploadFolder = ‘upload’; // 圖片存放目錄 public $thumbFolder = ‘upload/thumb’; // 縮略圖存放目錄 public $thumbWidth = ”; // 縮略圖寬度 public $thumbHeight = ”; // 縮略圖高度 public $autoThumb = ”; // 是否自動生成縮略圖 public $error = ”; // 錯誤信息 public $imgPath = ”; // 上傳成功後的圖片位置 public $thumbPath = ”; // 上傳成功後的縮略圖位置 public $maxsize=”; // 說明:初始化,創建存放目錄 function __construct($uploadFolder = ‘upload’, $thumbFolder = ‘upload/thumb’){ $this-uploadFolder = $uploadFolder; $this-thumbFolder = $thumbFolder; $this-_mkdir(); } // 說明:上傳圖片,參數是input /的name屬性值;成功返回圖片的相對URL,失敗返回FALSE和錯誤信息(在$this-error里) // bool/sting upload(string $html_tags_input_attrib_name); function upload($inputName){ // 上傳操作,參數是input標籤的name屬性。 if ($this-error){ // 如果有錯,直接返回(例如_mkdir) return FALSE; } if(!$_FILES[$inputName][“name”]){ $this-error = ‘沒有上傳圖片’; return FALSE; } //檢測文件大小 if($_FILES[$inputName][“size”] ($this-maxsize*1024)){ $this-error = ‘上傳文件’.$inputName.’太大,最大支持’.ceil($this-maxsize/1024).’kb的文件’; return FALSE; } if($_FILES[$inputName][“name”]){ $isUpFile = $_FILES[$inputName][‘tmp_name’]; if (is_uploaded_file($isUpFile)){ $imgInfo = $this-_getinfo($isUpFile); if (FALSE == $imgInfo){ return FALSE; } $extName = $imgInfo[‘type’]; $microSenond = floor(microtime()*10000);// 取一個毫秒級數字,4位。 $newFileName = $uploadFolder . ‘/’ . date(‘YmdHis’) . $microSenond . ‘.’ . $extName ; // 所上傳圖片的新名字。 $location = $this-uploadFolder . $newFileName; $result = move_uploaded_file($isUpFile, $location); if ($result) { if (TRUE == $this-autoThumb) { // 是否生成縮略圖 $thumb = $this-thumb($location, $this-thumbWidth, $this-thumbHeight); if (FALSE == $thumb) { return FALSE; } } //是否刪除原圖 if(TRUE==$this-srcDel) { @unlink ($location); } $this-imgPath = $location; return $location; }else{ $this-error = ‘移動臨時文件時出錯’; return FALSE; } }else{ $uploadError = $_FILES[$inputName][‘error’]; if (1 == $uploadError){ // 文件大小超過了php.ini中的upload_max_filesize $this-error = ‘文件太大,伺服器拒絕接收大於’ . ini_get(‘upload_max_filesize’) . ‘的文件’; return FALSE; }elseif (3 == $uploadError){ // 上傳了部分文件 $this-error = ‘上傳中斷,請重試’; return FALSE; }elseif (4 == $uploadError){ $this-error = ‘沒有文件被上傳’; return FALSE; }elseif (6 == $uploadError){ $this-error = ‘找不到臨時文件夾,請聯繫您的伺服器管理員’; return FALSE; }elseif (7 == $uploadError){ $this-error = ‘文件寫入失敗,請聯繫您的伺服器管理員’; return FALSE; }else{ if (0 != $uploadError){ $this-error = ‘未知上傳錯誤,請聯繫您的伺服器管理員’; return FALSE; } } // end if $uploadError } // end if is_uploaded_file else } // end if $_FILES[$inputName][“name”] } // 說明:獲取圖片信息,參數是上傳後的臨時文件,成功返回數組,失敗返回FALSE和錯誤信息 // array/bool _getinfo(string $upload_tmp_file) private function _getinfo($img){ if (!file_exists($img)){ $this-error = ‘找不到圖片,無法獲取其信息’; return FALSE; } $tempFile = @fopen($img, “rb”); $bin = @fread($tempFile, 2); //只讀2位元組 @fclose($tempFile); $strInfo = @unpack(“C2chars”, $bin); $typeCode = intval($strInfo[‘chars1’] . $strInfo[‘chars2’]); $fileType = ”; switch ($typeCode){ // 6677:bmp 255216:jpg 7173:gif 13780:png 7790:exe 8297:rar 8075:zip tar:109121 7z:55122 gz 31139 case ‘255216’: $fileType = ‘jpg’; break; case ‘6677’: $fileType = ‘bmp’; break; case ‘7173’: $fileType = ‘gif’; break; case ‘13780’: $fileType = ‘png’; break; default: $fileType = ‘unknown’; } if ($fileType == ‘jpg’ || $fileType == ‘gif’ || $fileType == ‘png’ || $fileType == ‘bmp’){ $imageInfo = getimagesize($img); $imgInfo[‘size’] = $imageInfo[‘bits’]; $imgInfo[“type”] = $fileType; $imgInfo[“width”] = $imageInfo[0]; $imgInfo[“height”] = $imageInfo[1]; return $imgInfo; }else{ // 非圖片類文件信息 $this-error = ‘圖片類型錯誤’; return FALSE; } } // end _getinfo // 說明:生成縮略圖,等比例縮放或拉伸 // bool/string thumb(string $uploaded_file, int $thumbWidth, int $thumbHeight, string $thumbTail); function thumb($img, $thumbWidth = 300, $thumbHeight = 200,$thumbTail = ‘_thumb’) { $filename = $img; // 保留一個名字供新的縮略圖名字使用 $imgInfo = $this-_getinfo($img,$i); if(FALSE == $imgInfo) { return FALSE; } $imgType = $imgInfo[‘type’]; switch ($imgType) { // 創建一個圖,並給出擴展名 case “jpg” : $img = imagecreatefromjpeg($img); $extName = ‘jpg’; break; case ‘gif’ : $img = imagecreatefromgif($img); $extName = ‘gif’; break; case ‘bmp’ : $img = imagecreatefromgif($img); $extName = ‘bmp’; break; case ‘png’ : $img = imagecreatefrompng($img); $extName = ‘png’; break; default : // 如果類型錯誤,生成一張空白圖 $img = imagecreate($thumbWidth,$thumbHeight); imagecolorallocate($img,0x00,0x00,0x00); $extName = ‘jpg’; } // 縮放後的圖片尺寸(小則拉伸,大就縮放) $imgWidth = $imgInfo[‘width’]; $imgHeight = $imgInfo[‘height’]; if($imgHeight $imgWidth) { // 豎圖 $newHeight = $thumbHeight; $newWidth = ceil($imgWidth / ($imgHeight / $thumbHeight )); } else if($imgHeight $imgWidth) { // 橫圖 $newHeight = ceil($imgHeight / ($imgWidth / $thumbWidth )); $newWidth = $thumbWidth; } else if($imgHeight == $imgWidth) { // 等比例圖 $newHeight = $thumbWidth; $newWidth = $thumbWidth; } $bgimg = imagecreatetruecolor($newWidth,$newHeight); $bg = imagecolorallocate($bgimg,0x00,0x00,0x00); imagefill($bgimg,0,0,$bg); $sampled = imagecopyresampled($bgimg,$img,0,0,0,0,$newWidth,$newHeight,$imgWidth,$imgHeight); if(!$sampled ) { $this-error = ‘縮略圖生成失敗’; $this-path=$this-uploadFolder . ‘/’ . $filename; return FALSE; } $filename = basename($filename); $newFileName = substr($filename, 0, strrpos($filename, “.”)) . $thumbTail . ‘.’ . $extName ; // 新名字 $thumbPath = $this-thumbFolder . ‘/’ . $newFileName; switch ($extName){ case ‘jpg’: $result = imagejpeg($bgimg, $thumbPath); break; case ‘gif’: $result = imagegif($bgimg, $thumbPath); break; case ‘png’: $result = imagepng($bgimg, $thumbPath); break; default: // 上邊判斷類型出錯時會創建一張空白圖,並給出擴展名為jpg $result = imagejpeg($bgimg, $thumbPath); } if ($result) { $this-thumbPath = $thumbPath; $this-path=$this-uploadFolder . ‘/’ . $filename; return $thumbPath; } else { $this-error = ‘縮略圖創建失敗’; $this-path=$this-uploadFolder . ‘/’ . $filename; return FALSE; } } // end thumb // 說明:創建圖片的存放目錄 private function _mkdir() { // 創建圖片上傳目錄和縮略圖目錄 if(!is_dir($this-uploadFolder)) { $dir = explode(‘/’, $this-uploadFolder); foreach($dir as $v) { if($v) { $d .= $v . ‘/’; if(!is_dir($d)) { $state = mkdir($d); if(!$state) { $this-error = ‘在創建目錄’ . $d . ‘時出錯!’; } } } } } if(!is_dir($this-thumbFolder) TRUE == $this-autoThumb) { $dir = explode(‘/’, $this-thumbFolder); foreach($dir as $v) { if($v) { $d .= $v . ‘/’; if(!is_dir($d)) { $state = mkdir($d); if(!$state) { $this-error = ‘在創建目錄’ . $d . ‘時出錯!’; } } } } } } } ?
能直接用的PHP生成縮略圖的程序(要求簡單)
?php
/*構造函數-生成縮略圖+水印,參數說明:
$srcFile-圖片文件名,
$dstFile-另存文件名,
$markwords-水印文字,
$markimage-水印圖片,
$dstW-圖片保存寬度,
$dstH-圖片保存高度,
$rate-圖片保存品質*/
makethumb(“a.jpg”,”b.jpg”,”50″,”50″);
function makethumb($srcFile,$dstFile,$dstW,$dstH,$rate=100,$markwords=null,$markimage=null)
{
$data = GetImageSize($srcFile);
switch($data[2])
{
case 1:
$im=@ImageCreateFromGIF($srcFile);
break;
case 2:
$im=@ImageCreateFromJPEG($srcFile);
break;
case 3:
$im=@ImageCreateFromPNG($srcFile);
break;
}
if(!$im) return False;
$srcW=ImageSX($im);
$srcH=ImageSY($im);
$dstX=0;
$dstY=0;
if ($srcW*$dstH$srcH*$dstW)
{
$fdstH = round($srcH*$dstW/$srcW);
$dstY = floor(($dstH-$fdstH)/2);
$fdstW = $dstW;
}
else
{
$fdstW = round($srcW*$dstH/$srcH);
$dstX = floor(($dstW-$fdstW)/2);
$fdstH = $dstH;
}
$ni=ImageCreateTrueColor($dstW,$dstH);
$dstX=($dstX0)?0:$dstX;
$dstY=($dstX0)?0:$dstY;
$dstX=($dstX($dstW/2))?floor($dstW/2):$dstX;
$dstY=($dstY($dstH/2))?floor($dstH/s):$dstY;
$white = ImageColorAllocate($ni,255,255,255);
$black = ImageColorAllocate($ni,0,0,0);
imagefilledrectangle($ni,0,0,$dstW,$dstH,$white);// 填充背景色
ImageCopyResized($ni,$im,$dstX,$dstY,0,0,$fdstW,$fdstH,$srcW,$srcH);
if($markwords!=null)
{
$markwords=iconv(“gb2312″,”UTF-8”,$markwords);
//轉換文字編碼
ImageTTFText($ni,20,30,450,560,$black,”simhei.ttf”,$markwords); //寫入文字水印
//參數依次為,文字大小|偏轉度|橫坐標|縱坐標|文字顏色|文字類型|文字內容
}
elseif($markimage!=null)
{
$wimage_data = GetImageSize($markimage);
switch($wimage_data[2])
{
case 1:
$wimage=@ImageCreateFromGIF($markimage);
break;
case 2:
$wimage=@ImageCreateFromJPEG($markimage);
break;
case 3:
$wimage=@ImageCreateFromPNG($markimage);
break;
}
imagecopy($ni,$wimage,500,560,0,0,88,31); //寫入圖片水印,水印圖片大小默認為88*31
imagedestroy($wimage);
}
ImageJpeg($ni,$dstFile,$rate);
ImageJpeg($ni,$srcFile,$rate);
imagedestroy($im);
imagedestroy($ni);
}
?
thinkphp 3.1.2生成縮略圖 image類怎麼引入
Thinkphp調用Image類生成縮略圖的方法具體分析如下:
Thinkphp的Image類 在ThinkPHP/Extend/Library/ORG/Util/Image.class.php中。
調用方法如下:
?1234567 import(“ORG.Util.Image”); $Img = new Image();//實例化圖片類對象 $image_path = ‘./圖片路徑’; //若當前php文件在Thinkphp的中APP_PATH路徑中 //’./’就是index.php的上一級文件。 //因為APP_PATH是通過index.php定義和載入的。 $image_info = $Img::getImageInfo($image_path);//獲取圖片信息
getImageInfo方法會獲取圖片的width,height,type,size,mime等信息。
縮略圖的生成很簡單。
參數需要img_path(原圖路徑),thumb_name(縮略圖名,包含路徑),thumb_type(圖片類型),Max_width(寬),Max_height(高):
?12 //生成縮略圖: $Img::thumb2($img_path,$thumb_name,$thumb_type,$Max_width,$Max_height);
需要注意的是,縮略圖的寬和高不能比原圖的大,不然就會生成失敗
php怎麼生成縮略圖
給你個函數吧
// *****生成縮略圖*****
// 只考慮jpg,png,gif格式
// $srcImgPath 源圖象路徑
// $targetImgPath 目標圖象路徑
// $targetW 目標圖象寬度
// $targetH 目標圖象高度
function makeThumbnail($srcImgPath,$targetImgPath,$targetW,$targetH)
{
$imgSize = GetImageSize($srcImgPath);
$imgType = $imgSize[2];
//@ 使函數不向頁面輸出錯誤信息
switch ($imgType)
{
case 1:
$srcImg = @ImageCreateFromGIF($srcImgPath);
break;
case 2:
$srcImg = @ImageCreateFromJpeg($srcImgPath);
break;
case 3:
$srcImg = @ImageCreateFromPNG($srcImgPath);
break;
}
//取源圖象的寬高
$srcW = ImageSX($srcImg);
$srcH = ImageSY($srcImg);
if($srcW$targetW || $srcH$targetH)
{
$targetX = 0;
$targetY = 0;
if ($srcW $srcH)
{
$finaW=$targetW;
$finalH=round($srcH*$finaW/$srcW);
$targetY=floor(($targetH-$finalH)/2);
}
else
{
$finalH=$targetH;
$finaW=round($srcW*$finalH/$srcH);
$targetX=floor(($targetW-$finaW)/2);
}
//function_exists 檢查函數是否已定義
//ImageCreateTrueColor 本函數需要GD2.0.1或更高版本
if(function_exists(“ImageCreateTrueColor”))
{
$targetImg=ImageCreateTrueColor($targetW,$targetH);
}
else
{
$targetImg=ImageCreate($targetW,$targetH);
}
$targetX=($targetX0)?0:$targetX;
$targetY=($targetX0)?0:$targetY;
$targetX=($targetX($targetW/2))?floor($targetW/2):$targetX;
$targetY=($targetY($targetH/2))?floor($targetH/2):$targetY;
//背景白色
$white = ImageColorAllocate($targetImg, 255,255,255);
ImageFilledRectangle($targetImg,0,0,$targetW,$targetH,$white);
/*
PHP的GD擴展提供了兩個函數來縮放圖象:
ImageCopyResized 在所有GD版本中有效,其縮放圖象的演算法比較粗糙,可能會導致圖象邊緣的鋸齒。
ImageCopyResampled 需要GD2.0.1或更高版本,其像素插值演算法得到的圖象邊緣比較平滑,
該函數的速度比ImageCopyResized慢。
*/
if(function_exists(“ImageCopyResampled”))
{
ImageCopyResampled($targetImg,$srcImg,$targetX,$targetY,0,0,$finaW,$finalH,$srcW,$srcH);
}
else
{
ImageCopyResized($targetImg,$srcImg,$targetX,$targetY,0,0,$finaW,$finalH,$srcW,$srcH);
}
switch ($imgType) {
case 1:
ImageGIF($targetImg,$targetImgPath);
break;
case 2:
ImageJpeg($targetImg,$targetImgPath);
break;
case 3:
ImagePNG($targetImg,$targetImgPath);
break;
}
ImageDestroy($srcImg);
ImageDestroy($targetImg);
}
else //不超出指定寬高則直接複製
{
copy($srcImgPath,$targetImgPath);
ImageDestroy($srcImg);
}
}
代碼已經測試,成功運行!
php創建縮略圖問題
可能你找的這些處理函數(類)功能比較強大,所以會有複雜的感覺。如果只是單純的放大縮小,使用 GD 庫,還是比較簡單的。php 手冊里有一個例子,使用 imagecopyresized 函數。完整的例子如下,你也可以直接看手冊獲取更多的信息,希望對你有幫助。
// PHP 手冊 imagecopyresized 函數的例子
// File and new size
$filename = ‘test.jpg’;
$percent = 0.5;
// Content type
header(‘Content-Type: image/jpeg’);
// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;
// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);
// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// Output
imagejpeg($thumb);
用PHP怎麼生成高質量的縮略圖
ImageMagick沒用過,一般直接用內置的GD庫,沒有發現你說的這麼嚴重的失真問題。
利用GD庫創建縮略圖的大致思路如下:
依據設定的尺寸創建真彩色畫布$im=createtruecolor(120,90);
讀取原始文件尺寸,按照原始尺寸的寬度和高度比例,計算出縮略圖的大小(可能與給定的尺寸有一定的偏差)
將原始圖像拷貝並縮放到創建的真彩色縮略圖畫布上。
輸出縮略圖文件。
可能就是因為利用的是這個真彩色,縮略圖效果還湊合,也不是說絕對不失真的
你可以去後盾人平台看看,裡面的東西不錯
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/200964.html