當前位置:首頁 » 編程語言 » php編輯圖片

php編輯圖片

發布時間: 2025-05-16 12:42:05

『壹』 php圖片可以等比例的縮放嗎

可以。

等比例縮放的方法是:

1、載入選區--自由變換。如下圖:

2、按住shift+alt鍵,使用滑鼠調整大小,這種情況下,選區會按照等比例的方法進行縮放的。

『貳』 PHP圖片生成

給你一個php 圖像處理類,完全能實現你的功能,你自己研究一下吧

<?php
class image
{
var $w_pct = 50; //透明度
var $w_quality = 80; //質量
var $w_minwidth = 300; //最小寬
var $w_minheight = 300; //最小高
var $thumb_enable; //是否生成縮略圖
var $watermark_enable; //是否生水印
var $interlace = 0; //圖像是否為隔行掃描的
var $fontfile; //字體文件
var $w_img ; //默認水印圖

function __construct()
{
global $SITE_CONFING;
$this->thumb_enable = $SITE_CONFING['thumb_enable'];
$this->watermark_enable = $SITE_CONFING['watermark_enable'];
$this->set($SITE_CONFING['watermark_minwidth'], $SITE_CONFING['watermark_minheight'], $SITE_CONFING['watermark_quality'], $SITE_CONFING['watermark_pct'], $SITE_CONFING['watermark_fontfile'],$SITE_CONFING['watermark_img']);
}

function image()
{
$this->__construct();
}

function set($w_minwidth = 300, $w_minheight = 300, $w_quality = 80, $w_pct = 100,$fontfile,$w_img)
{
$this->w_minwidth = $w_minwidth;
$this->w_minheight = $w_minheight;
$this->w_quality = $w_quality;
$this->w_pct = $w_pct;
$this->fontfile = $fontfile;
$this->w_img = $w_img;
}

function info($img)
{
$imageinfo = getimagesize($img); //返回圖像信息數組 0=>寬的像素 1=>高的像素 2=>是圖像類型的標記 3 =>是文本字元串,內容為「height="yyy" width="xxx"」,
if($imageinfo === false) return false;
$imagetype = strtolower(substr(image_type_to_extension($imageinfo[2]),1)); //獲取圖像文件類型 $imageinfo[2]是圖像類型的標記
$imagesize = filesize($img); //圖像大小
$info = array(
'width'=>$imageinfo[0],
'height'=>$imageinfo[1],
'type'=>$imagetype,
'size'=>$imagesize,
'mime'=>$imageinfo['mime']
);
return $info;
}

function thumb($image, $filename = '', $maxwidth = 200, $maxheight = 50, $suffix='_thumb', $autocut = 0)
{
if(!$this->thumb_enable || !$this->check($image)) return false;
$info = $this->info($image); //獲取圖片信息
if($info === false) return false;
$srcwidth = $info['width']; //源圖寬
$srcheight = $info['height']; //源圖高
$pathinfo = pathinfo($image);
$type = $pathinfo['extension']; //取得擴展名
if(!$type) $type = $info['type']; //如果沒有取到,用$info['type']
$type = strtolower($type);
unset($info);
$scale = min($maxwidth/$srcwidth, $maxheight/$srcheight); //獲取縮略比例
//獲取按照源圖的比列
$createwidth = $width = (int)($srcwidth*$scale); //取得縮略寬
$createheight = $height = (int)($srcheight*$scale); //取得縮略高
$psrc_x = $psrc_y = 0;
if($autocut) //按照縮略圖的比例來獲取
{
if($maxwidth/$maxheight<$srcwidth/$srcheight && $maxheight>=$height) //如果縮略圖按比列比源圖窄的話
{
$width = $maxheight/$height*$width; //寬按照相應比例做處理
$height = $maxheight; //高不變
}
elseif($maxwidth/$maxheight>$srcwidth/$srcheight && $maxwidth>=$width)//如果縮略圖按比列比源圖寬的話
{
$height = $maxwidth/$width*$height;
$width = $maxwidth;
}
$createwidth = $maxwidth;
$createheight = $maxheight;
}
$createfun = 'imagecreatefrom'.($type=='jpg' ? 'jpeg' : $type); //找到不同的圖像處理函數
$srcimg = $createfun($image); //新建圖像
if($type != 'gif' && function_exists('imagecreatetruecolor'))
$thumbimg = imagecreatetruecolor($createwidth, $createheight); //新建一個真彩色圖像
else
$thumbimg = imagecreate($width, $height); //新建一個基於調色板的圖像

if(function_exists('imageresampled')) //重采樣拷貝部分圖像並調整大小,真彩
//imageresampled(新圖,源圖,新圖左上角x距離,新圖左上角y距離,源圖左上角x距離,源圖左上角y距離,新圖寬,新圖高,源圖寬,源圖高)
imageresampled($thumbimg, $srcimg, 0, 0, $psrc_x, $psrc_y, $width, $height, $srcwidth, $srcheight);
else //拷貝部分圖像並調整大小,調色板
imageresized($thumbimg, $srcimg, 0, 0, $psrc_x, $psrc_y, $width, $height, $srcwidth, $srcheight);
if($type=='gif' || $type=='png')
{
//imagecolorallocate 為一幅圖像分配顏色
$background_color = imagecolorallocate($thumbimg, 0, 255, 0); // 給基於調色板的圖像填充背景色, 指派一個綠色
// imagecolortransparent 將某個顏色定義為透明色
imagecolortransparent($thumbimg, $background_color); // 設置為透明色,若注釋掉該行則輸出綠色的圖
}
// imageinterlace 激活或禁止隔行掃描
if($type=='jpg' || $type=='jpeg') imageinterlace($thumbimg, $this->interlace);
$imagefun = 'image'.($type=='jpg' ? 'jpeg' : $type);
//imagejpeg imagegif imagepng
if(empty($filename)) $filename = substr($image, 0, strrpos($image, '.')).$suffix.'.'.$type; //獲取文件名
//aaa.gif aaa_thumb.gif
$imagefun($thumbimg, $filename); //新建圖像
imagedestroy($thumbimg); //銷毀縮略圖
imagedestroy($srcimg); //銷毀源圖
return $filename;
}
//watermark(源圖,生成文件,生成位置,水印文件,水印文本,背景色)
function watermark($source, $target = '', $w_pos = 0, $w_img = '', $w_text = '', $w_font = 12, $w_color = '#cccccc')
{
if(!$this->watermark_enable || !$this->check($source)) return false;
if(!$target) $target = $source;
if ($w_img == '' && $w_text == '')
$w_img = $this->w_img;
$source_info = getimagesize($source);
$source_w = $source_info[0]; //獲取寬
$source_h = $source_info[1]; //獲取高
if($source_w < $this->w_minwidth || $source_h < $this->w_minheight) return false; //寬和高達不到要求直接返回
switch($source_info[2]) //新建圖片
{
case 1 :
$source_img = imagecreatefromgif($source);
break;
case 2 :
$source_img = imagecreatefromjpeg($source);
break;
case 3 :
$source_img = imagecreatefrompng($source);
break;
default :
return false;
}
if(!empty($w_img) && file_exists($w_img)) //水印文件
{
$ifwaterimage = 1; //是否水印圖
$water_info = getimagesize($w_img); //水印信息
$width = $water_info[0];
$height = $water_info[1];
switch($water_info[2])
{
case 1 :
$water_img = imagecreatefromgif($w_img);
break;
case 2 :
$water_img = imagecreatefromjpeg($w_img);
break;
case 3 :
$water_img = imagecreatefrompng($w_img);
break;
default :
return;
}
}
else
{
$ifwaterimage = 0;
//imagettfbbox 本函數計算並返回一個包圍著 TrueType 文本范圍的虛擬方框的像素大小。
//imagettfbbox ( 字體大小, 字體角度, 字體文件,文件 )
$temp = imagettfbbox(ceil($w_font*1.2), 0, $this->fontfile, $w_text);//取得使用 truetype 字體的文本的范圍
$width = $temp[4] - $temp[6]; //右上角 X 位置 - 左上角 X 位置
$height = $temp[3] - $temp[5]; //右下角 Y 位置- 右上角 Y 位置
unset($temp);
}
switch($w_pos)
{
case 0: //隨機位置
$wx = rand(0,($source_w - $width));
$wy = rand(0,($source_h - $height));
break;
case 1: //左上角
$wx = 5;
$wy = 5;
break;
case 2: //上面中間位置
$wx = ($source_w - $width) / 2;
$wy = 0;
break;
case 3: //右上角
$wx = $source_w - $width;
$wy = 0;
break;
case 4: //左面中間位置
$wx = 0;
$wy = ($source_h - $height) / 2;
break;
case 5: //中間位置
$wx = ($source_w - $width) / 2;
$wy = ($source_h - $height) / 2;
break;
case 6: //底部中間位置
$wx = ($source_w - $width) / 2;
$wy = $source_h - $height;
break;
case 7: //左下角
$wx = 0;
$wy = $source_h - $height;
break;
case 8: //右面中間位置
$wx = $source_w - $width;
$wy = ($source_h - $height) /2;
break;
case 9: //右下角
$wx = $source_w - $width;
$wy = $source_h - $height ;
break;
default: //隨機
$wx = rand(0,($source_w - $width));
$wy = rand(0,($source_h - $height));
break;
}
if($ifwaterimage) //如果有水印圖
{
//imagemerge 拷貝並合並圖像的一部分
//參數(源圖,水印圖,拷貝到源圖x位置,拷貝到源圖y位置,從水印圖x位置,從水印圖y位置,高,寬,透明度)
imagemerge($source_img, $water_img, $wx, $wy, 0, 0, $width, $height, $this->w_pct);
}
else
{
if(!empty($w_color) && (strlen($w_color)==7))
{
$r = hexdec(substr($w_color,1,2)); //獲取紅色
$g = hexdec(substr($w_color,3,2)); //獲取綠色
$b = hexdec(substr($w_color,5)); //獲取藍色
}
else
{
return;
}
//imagecolorallocate 基於調色板的圖像填充背景色
//imagestring 水平地畫一行字元串
//imagestring(源圖,字體大小,位置X,位置Y,文字,顏色)
//參數($image, float $size, float $angle, int $x, int $y, int $color, string $fontfile, string $text)
imagettftext($source_img,$w_font,0,$wx,$wy,imagecolorallocate($source_img,$r,$g,$b),$this->fontfile,$w_text);
//imagestring($source_img,$w_font,$wx,$wy,$w_text,imagecolorallocate($source_img,$r,$g,$b));
}
//輸出到文件或者瀏覽器
switch($source_info[2])
{
case 1 :
imagegif($source_img, $target); //以 GIF 格式將圖像輸出到瀏覽器或文件
break;
case 2 :
imagejpeg($source_img, $target, $this->w_quality); //以 JPEG 格式將圖像輸出到瀏覽器或文件
break;
case 3 :
imagepng($source_img, $target); //以 PNG 格式將圖像輸出到瀏覽器或文件
break;
default :
return;
}
if(isset($water_info))
{
unset($water_info); //銷毀
}
if(isset($water_img))
{
imagedestroy($water_img); //銷毀
}
unset($source_info);
imagedestroy($source_img);
return true;
}
//gd庫必須存在,後綴為jpg|jpeg|gif|png,文件存在,imagecreatefromjpeg或者imagecreatefromgif存在
function check($image)
{
return extension_loaded('gd') &&
preg_match("/\.(jpg|jpeg|gif|png)/i", $image, $m) &&
file_exists($image) &&
function_exists('imagecreatefrom'.($m[1] == 'jpg' ? 'jpeg' : $m[1]));
//imagecreatefromjpeg
//imagecreatefromgif
//imagecreatefrompng
}
}

/**
縮略圖
1.新建一個圖像資源 通過 imagecreatefromgif imagecreatefromjpeg imagecreatefrompng
2.imageresampled 拷貝圖像,並調整大小

水印:圖片水印,文字水印
1. 創建圖像
2.加水印
圖片水印:imagemerge 把2張圖合並在一起
文字水印:imagettftext 向圖像寫入文字

*/
?>

『叄』 用php代碼怎麼以背景圖片加上文字生成新的圖片,然後在標題處絕對調用該圖片

<?php
ob_clean(); //清除輸出緩存
header("Content-type:image/jpeg"); //設置輸出類型
$img="images/test.jpg"; //背景圖片名
if(isset($_GET["img"]))$img=$_GET["img"]; //也可以通過img參數傳入
$im=imagecreatefromjpeg($img); //讀入背景圖片
$text="文字內容"; //要加上的文字內容
if(isset($_GET["text"]))$text=$_GET["text"]; //也可以通過text參數傳入
$fontFile="xxx.ttf"; //字體文件名,必須要
$fontSize=36; //字體尺寸
$fontColor=ImageColorAllocate($im,0,0,0); //字體顏色,這里是黑色
$textAngle=0; //文字顯示的角度,0表示水平顯示
$textLeft=20; //文字顯示的x坐標
$textTop=60; //文字顯示的y坐標
imagefttext($im,$fontSize,$textAngle,$textLeft,$textTop,$fontColor,$fontFile,$text); //把文字覆蓋到圖片上
Imagejpeg($im); //輸出圖片
ImageDestroy($im); //銷毀圖片
?>
把以上文字保存為php文件,比如 img.php
然後在需要調用圖片的地方用 <img src="img.php?img=背景圖片文件路徑&text=要加上的文字"/> 來調用
比如 <img src="img.php?img=images/back.jpg&text=你好"/>

『肆』 php怎麼修改圖片的尺寸大小並且覆蓋原圖

<?php
$imgsrc = "http://www.nowamagic.net/images/3.jpg";
$width =
780;
$height = 420;
resizejpg($imgsrc,$imgdst,$width,$height);

function resizejpg($imgsrc,$imgdst,$imgwidth,$imgheight)
{
//$imgsrc
jpg格式圖像路徑 $imgdst jpg格式圖像保存文件名 $imgwidth要改變的寬度 $imgheight要改變的高度

//取得圖片的寬度,高度值
$arr = getimagesize($imgsrc);
header("Content-type:
image/jpg");
$imgWidth = $imgwidth;
$imgHeight = $imgheight;
//
Create image and define colors
$imgsrc = imagecreatefromjpeg($imgsrc);

$image = imagecreatetruecolor($imgWidth, $imgHeight); //創建一個彩色的底圖

imageresampled($image, $imgsrc, 0, 0, 0, 0,$imgWidth,$imgHeight,$arr[0],
$arr[1]);
imagepng($image);
imagedestroy($image);
}
?>

『伍』 php圖片加字將字旋轉位置

根據文字所在圖片的位置方向,計算文字的位置。
php給圖片添加文字水印,可控制位置,旋轉,多行文字,主要包括php給圖片添加文字水印可控制位置,旋轉,多行文字使用實例、應用技巧、基本知識點總結和需要注意事項。

『陸』 PHP 長方形圖片 不變形 裁剪 生成 正方形

在處理圖片時,我們經常會遇到將長方形圖片裁剪為正方形的需求。以PHP為例,我們可以使用imageresampled()函數實現這一目標。假設我們有一個150x150像素的圖片,需要將其裁剪成200x200像素的正方形,可以使用如下代碼:

$x = (200-150)/2; imageresampled($thumb, $img_r2, 0, 0, $x, 0, $thumb_w, $thumb_h, 150, 150);

其中,$thumb表示目標圖像,$img_r2表示源圖像,$x計算出的值用於確定源圖像的起始點,$thumb_w和$thumb_h為目標圖像的寬度和高度,150和150分別為源圖像的寬度和高度。

imageresampled()函數是PHP中用於重采樣拷貝圖像區域並調整大小的關鍵函數。其功能包括:

1. 將一幅圖像中的一塊正方形區域拷貝到另一個圖像中。

2. 平滑地插入像素值。

3. 減小了圖像的大小而仍然保持了極大的清晰度。

如果函數執行成功,返回值為TRUE,否則返回FALSE。

dst_image和src_image分別是目標圖像和源圖像的標識符。

當源和目標的寬度和高度不同時,會進行相應的圖像收縮和拉伸。坐標指的是左上角。

此外,該函數還可以用於在同一幅圖內部拷貝區域,但如果區域交迭的話則結果不可預知。

需要注意的是,對於調色板圖像,重采樣或過濾圖像通常需要多於255種顏色,這可能會導致計算新的被重采樣的像素及其顏色時產生誤差。對於這種情況,建議使用真彩色圖像作為目標圖像,例如使用imagecreatetruecolor()創建的。

最後,imageresampled()函數需要GD庫2.0.1或更高版本(推薦2.0.28及以上版本)支持。

『柒』 求php圖片縮放處理函數

在PHP網站開發過程中,如果建立的網站涉及大量的圖片處理,必然涉及到圖片的上傳和縮放,保持圖片不失真,進行圖片縮放。使用之前需要下載安裝GD庫,以支持PHP圖片處理。下面結合代碼講解具體的PHP圖片縮放處理的思路。
function resizeImage($im,$maxwidth,$maxheight,$name,$filetype)
{
$pic_width = imagesx($im);
$pic_height = imagesy($im);

if(($maxwidth && $pic_width > $maxwidth) ($maxheight && $pic_height > $maxheight))
{
if($maxwidth && $pic_width>$maxwidth)
{
$widthratio = $maxwidth/$pic_width;
$resizewidth_tag = true;
}

if($maxheight && $pic_height>$maxheight)
{
$heightratio = $maxheight/$pic_height;
$resizeheight_tag = true;
}

if($resizewidth_tag && $resizeheight_tag)
{
if($widthratio<$heightratio)
$ratio = $widthratio;
else
$ratio = $heightratio;
}

if($resizewidth_tag && !$resizeheight_tag)
$ratio = $widthratio;
if($resizeheight_tag && !$resizewidth_tag)
$ratio = $heightratio;

$newwidth = $pic_width * $ratio;
$newheight = $pic_height * $ratio;

if(function_exists("imageresampled"))
{
$newim = imagecreatetruecolor($newwidth,$newheight);
imageresampled($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);
}
else
{
$newim = imagecreate($newwidth,$newheight);
imageresized($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);
}

$name = $name.$filetype;
imagejpeg($newim,$name);
imagedestroy($newim);
}
else
{
$name = $name.$filetype;
imagejpeg($im,$name);
}
}
參數說明:
$im 圖片對象,應用函數之前,需要用imagecreatefromjpeg()讀取圖片對象,如果PHP環境支持PNG,GIF,也可使用imagecreatefromgif(),imagecreatefrompng();
$maxwidth 定義生成圖片的最大寬度(單位:像素)
$maxheight 生成圖片的最大高度(單位:像素)
$name 生成的圖片名
$filetype 最終生成的圖片類型(.jpg/.png/.gif)
代碼注釋:
第3~4行:讀取需要縮放的圖片實際寬高
第8~26行:通過計算實際圖片寬高與需要生成圖片的寬高的壓縮比例最終得出進行圖片縮放是根據寬度還是高度進行縮放,當前程序是根據寬度進行圖片縮放。如果想根據高度進行圖片縮放,可以將第22行的語句改成$widthratio>$heightratio
第28~31行:如果實際圖片的長度或者寬度小於規定生成圖片的長度或者寬度,則要麼根據長度進行圖片縮放,要麼根據寬度進行圖片縮放。
第33~34行:計算最終縮放生成的圖片長寬。
第36~45行:根據計算出的最終生成圖片的長寬改變圖片大小,有兩種改變圖片大小的方法:ImageCopyResized()函數在所有GD版本中有效,但其縮放圖像的演算法比較粗糙。ImageCopyResamples(),其像素插值演算法得到的圖像邊緣比較平滑,但該函數的速度比ImageCopyResized()慢。
第47~49行:最終生成經過處理後的圖片,如果需要生成GIF或PNG,需要將imagejpeg()函數改成imagegif()或imagepng()
第51~56行:如果實際圖片的長寬小於規定生成的圖片長寬,則保持圖片原樣,同理,如果需要生成GIF或PNG,需要將imagejpeg()函數改成imagegif()或imagepng()。
特別說明:
GD庫1.6.2版以前支持GIF格式,但因GIF格式使用LZW演演算法牽涉專利權,因此在GD1.6.2版之後不支持GIF的格式。如果是WINDOWS的環境,只要進入PHP.INI文件找到extension=php_gd2.dll,將#去除,重啟APACHE即可。如果是Linux環境,又想支持GIF,PNG,JPEG,需要去下載libpng,zlib,以及freetype字體並安裝。
OK,PHP圖片壓縮函數完成,最後概述一下整個處理的思路:
通過計算實際圖片的長寬與規定生成圖片的長寬之間的縮放比例,根據實際的需求(按照寬度還是按照高度進行圖片縮放)計算出最終生成圖片的大小,然後應用PHP圖片處理函數對圖片進行處理,最後輸出圖片。
以上就是關於PHP圖片處理中如何對圖片進行壓縮並保持不失真的函數說明。

熱點內容
看演算法頭疼 發布:2025-05-16 16:56:41 瀏覽:798
給定文件編譯成dll文件 發布:2025-05-16 16:45:05 瀏覽:729
熔噴機的配置有哪些 發布:2025-05-16 16:45:04 瀏覽:720
20149月二級c語言 發布:2025-05-16 16:22:29 瀏覽:961
恐怖月亮伺服器ip 發布:2025-05-16 16:18:42 瀏覽:723
java核心技術1 發布:2025-05-16 16:18:01 瀏覽:729
安卓汽車盲區輔助哪裡下載 發布:2025-05-16 16:02:53 瀏覽:362
碼尚編程 發布:2025-05-16 15:55:39 瀏覽:446
android全局字體 發布:2025-05-16 15:53:19 瀏覽:574
jsp在伺服器編譯先轉譯成什麼 發布:2025-05-16 15:41:36 瀏覽:983