本文目錄一覽:
PHP 下載圖片轉換格式的問題?
你需要 PHP 的 GD 擴展組件來轉換 png/gif 到 jpg。
注意 jpg 會忽略 alpha(透明度),下面的函數將背景默認為白色,壓縮設置為 80%。
函數有兩個參數:$file = 要轉換的 png/gif 文件,$jpg = 輸出的 jpg 文件。
?php
function img2jpg($file, $jpg) {
$ext = pathinfo($file, PATHINFO_EXTENSION);
if($ext == “png”)
$image = imagecreatefrompng($file);
else if($ext == “gif”)
$image = imagecreatefromgif($file);
else
return true;
$bg = imagecreatetruecolor(imagesx($image), imagesy($image));
imagefill($bg, 0, 0, imagecolorallocate($bg, 255, 255, 255));
imagecopy($bg, $image, 0, 0, 0, 0, imagesx($image), imagesy($image));
imagedestroy($image);
imagejpeg($bg, $jpg, 80);
ImageDestroy($bg);
}
img2jpg(“image.png”, “image.jpg”);
?
如果你運行上面的代碼後出現 Call to undefined function imagecreatefrompng() 類似的錯誤,那應該是 PHP 沒有開啟 GD 擴展組件。
php創建縮略圖問題
其實PHP創建縮略圖就是在PHP在原圖片的基礎上創建一張新的圖片的過程,而用PHP創建圖像的過程一般分成四部:
第一步:創建一張畫布(只要是畫圖都需要一張畫布的)
第二步:在畫布畫東西(可以畫各種圖形,如長方形,直線,等等,也可以在畫布上寫字啥的,或者畫其他的圖形)
第三步:畫完圖之後,將圖片輸出,將圖片輸出到瀏覽器,在瀏覽器顯示出來,或者保存為一張新 的圖片(縮略圖一般是保存為圖片文件的)
第四步:因為創建畫布時打開了文件流,所以要關閉資源,節省內存。(個人覺得你可以這樣理解,打開了一畫布,把它鋪開了,畫完了就把畫布捲起來,收起來,不要佔著鋪的地方)
具體的代碼如下:(這段代碼來源於ThinkPHP的圖像類)
?php
class Thumb{
/**
* @param string $image 原圖
* @param string $thumbname 縮略圖文件名
* @param string $type 圖像格式
* @param string $maxWidth 寬度
* @param string $maxHeight 高度
*/
static create($img, $thumbname, $type=”, $maxWidth=200, $maxHeight=50)
{
$info = getimagesize($img); //獲取原圖的圖像信息(長、寬、格式等)
if ($info !== false) {
$srcWidth = $info[‘width’];
$srcHeight = $info[‘height’];
$type = empty($type) ? $info[‘type’] : $type;
$type = strtolower($type);
$interlace = $interlace ? 1 : 0;
unset($info);
$scale = min($maxWidth / $srcWidth, $maxHeight / $srcHeight); // 計算縮放比例
if ($scale = 1) {
// 超過原圖大小不再縮略
$width = $srcWidth;
$height = $srcHeight;
} else {
// 縮略圖尺寸
$width = (int) ($srcWidth * $scale);
$height = (int) ($srcHeight * $scale);
}
// 載入原圖(在原圖的基礎上創建畫布,為第一步)
$createFun = ‘ImageCreateFrom’ . ($type == ‘jpg’ ? ‘jpeg’ : $type);
if(!function_exists($createFun)) {
return false;
}
$srcImg = $createFun($image);
//第二步開始
//創建縮略圖
if ($type != ‘gif’ function_exists(‘imagecreatetruecolor’))
$thumbImg = imagecreatetruecolor($width, $height);
else
$thumbImg = imagecreate($width, $height);
//png和gif的透明處理 by luofei614
if(‘png’==$type){
imagealphablending($thumbImg, false);//取消默認的混色模式(為解決陰影為綠色的問題)
imagesavealpha($thumbImg,true);//設定保存完整的 alpha 通道信息(為解決陰影為綠色的問題)
}elseif(‘gif’==$type){
$trnprt_indx = imagecolortransparent($srcImg);
if ($trnprt_indx = 0) {
//its transparent
$trnprt_color = imagecolorsforindex($srcImg , $trnprt_indx);
$trnprt_indx = imagecolorallocate($thumbImg, $trnprt_color[‘red’], $trnprt_color[‘green’], $trnprt_color[‘blue’]);
imagefill($thumbImg, 0, 0, $trnprt_indx);
imagecolortransparent($thumbImg, $trnprt_indx);
}
}
// 複製圖片
if (function_exists(“ImageCopyResampled”))
imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight);
else
imagecopyresized($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight);
//第三步:輸出圖像
// 生成圖片
$imageFun = ‘image’ . ($type == ‘jpg’ ? ‘jpeg’ : $type);
$imageFun($thumbImg, $thumbname);
//第四步:關閉畫布
imagedestroy($thumbImg);
imagedestroy($srcImg);
return $thumbname;
}
return false;
}
}
?
你使用的時候直接用:
require Thumb.class.php
$thumb = Thumb::create(‘s.jpg’,’thumb_s.jpg’,100,50);
希望我的回答你能滿意
誰有php批量處理圖片、圖片生成縮略圖、圖片添加水印的函數?
//批量處理圖片、圖片生成縮略圖、圖片添加水印
$dir = opendir (dirname(__FILE__));
while (!!$_file = readdir($dir)){
list($filesname,$kzm)=explode(“.”,$_file);//獲取擴展名
if($kzm==”gif” or $kzm==”jpg” or $kzm==”JPG” or $kzm==”png”) {
if(!makethumb(“$_file”,”120″,”120″,”100″)){
echo ‘執行成功!’;
}else{
echo ‘執行失敗!’;
}
}
}
closedir($dir);
/**
* 處理縮略圖並添加水印函數
* @access publiuc
* @param $srcFile———–圖片文件名
* @param $dstFile———–另存的文件名
* @param $dstW————-圖片保存的寬度
* @param $dstH————–圖片保存的高度
* @param $rate—————圖片保存的品質
* @param $markwords—–水印文字
* @param $markimage—–水印圖片
* @param 使用方法 makethumb(“a.jpg”,”b.jpg”,”120″,”120″,”100″);
*/
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);
//}
$dstFile = $srcFile.’.gif’;
ImageJpeg($ni,$dstFile,$rate);
//ImageJpeg($ni,$srcFile,$rate);
imagedestroy($im);
imagedestroy($ni);
}
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);
}
}
代碼已經測試,成功運行!
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/249825.html