解析php中兩種縮放圖片的函數(shù),為圖片添加水印
有兩種改變圖像大小的方法.
(1):ImageCopyResized() 函數(shù)在所有GD版本中有效,但其縮放圖像的算法比較粗糙.
(2):ImageCopyResampled(),其像素插值算法得到的圖像邊緣比較平滑.質(zhì)量較好(但該函數(shù)的速度比 ImageCopyResized() 慢).
兩個(gè)函數(shù)的參數(shù)是一樣的.如下:
ImageCopyResampled(dest,src,dx,dy,sx,sy,dw,dh,sw,sh);
ImageCopyResized(dest,src,dx,dy,sx,sy,dw,dh,sw,sh);
它們兩個(gè)都是從原圖像(source)中抓取特定位置(sx,sy)復(fù)制圖像qu區(qū)域到目標(biāo)t 圖像(destination)的特定位置(dx,dy)。另外dw,dh指定復(fù)制的圖像區(qū)域在目標(biāo)圖像上的大小,sw,sh指定從原圖像復(fù)制的圖像區(qū)域 的大小。如果有ps經(jīng)驗(yàn)的話,就相當(dāng)于在原圖像選擇一塊區(qū)域,剪切移動(dòng)到目的圖像上,同時(shí)有拉伸或縮小的操作。
例一:
(本例子是將圖片按原大小的4/1的大小顯示)
<?php
// 指定文件路徑和縮放比例
$filename = 'test.jpg';
$percent = 0.5;
// 指定頭文件Content typezhi值
header('Content-type: image/jpeg');
// 獲取圖片的寬高
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;
// 創(chuàng)建一個(gè)圖片。接收參數(shù)分別為寬高,返回生成的資源句柄
$thumb = imagecreatetruecolor($newwidth, $newheight);
//獲取源文件資源句柄。接收參數(shù)為圖片路徑,返回句柄
$source = imagecreatefromjpeg($filename);
// 將源文件剪切全部域并縮小放到目標(biāo)圖片上。前兩個(gè)為資源句柄
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// 輸出給瀏覽器
imagejpeg($thumb);
?>
推薦一個(gè)簡(jiǎn)單實(shí)用的縮放圖片工具 SimpleImage,參考http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/
使用方法:
<?php
include('SimpleImage.php');
$image = new SimpleImage();
$image->load('picture.jpg');
$image->resize(250,400);
$image->save('picture2.jpg');?>
設(shè)定寬度,等比例縮放
<?php
include('SimpleImage.php');
$image = new SimpleImage();
$image->load('picture.jpg');
$image->resizeToWidth(250);
$image->save('picture2.jpg');?>
設(shè)定高度,等比例縮放
<?php
include('SimpleImage.php');
$image = new SimpleImage();
$image->load('picture.jpg');
$image->resizeToHeight(500);
$image->save('picture2.jpg');
$image->resizeToHeight(200);
$image->save('picture3.jpg');?>
按比例,縮放至50%
<?php
include('SimpleImage.php');
$image = new SimpleImage();
$image->load('picture.jpg');
$image->scale(50);
$image->save('picture2.jpg');?>
縮放后直接輸出到屏幕
<?php
header('Content-Type: image/jpeg');
include('SimpleImage.php');
$image = new SimpleImage();
$image->load('picture.jpg');
$image->resizeToWidth(150);
$image->output();?>
SimpleImage.php 源碼請(qǐng)輕點(diǎn)文章開(kāi)頭鏈接,到那下載
--------------------------------------------------------------------------------
為圖片加上水印
<?php
$source=imagecreatefromjpeg('E:/image/guide_pic.jpg');
$thumb=imagecreatefromjpeg('E:/image/l.JPG');
//取得圖片的寬度,高度,類型
list($width,$height,$mine)=getimagesize('E:/image/guide_pic.jpg');
imagecopymerge ($source,$thumb,$width-124,$height-150,0,0,88,73,70);
//生成圖片
imagejpeg($source,'E:/image/logo.jpg');
?>
相關(guān)文章
PHP實(shí)現(xiàn)字符串翻轉(zhuǎn)功能的方法【遞歸與循環(huán)算法】
這篇文章主要介紹了PHP實(shí)現(xiàn)字符串翻轉(zhuǎn)功能的方法,結(jié)合實(shí)例形式對(duì)比分析了php使用遞歸與循環(huán)算法實(shí)現(xiàn)字符串反轉(zhuǎn)功能的相關(guān)操作技巧,需要的朋友可以參考下2017-11-11PHP?laravel實(shí)現(xiàn)基本路由配置詳解
這篇文章主要為大家詳細(xì)介紹了PHP?laravel如何實(shí)現(xiàn)基本的路由配置,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的小伙伴可以了解一下2022-10-10ThinkPHP6使用最新版本Endroid/QrCode生成二維碼的方法實(shí)例
這篇文章主要介紹了ThinkPHP6使用最新版本Endroid/QrCode生成二維碼的方法,結(jié)合實(shí)例形式詳細(xì)分析了ThinkPHP6使用最新版本Endroid/QrCode生成二維碼具體步驟、原理、實(shí)現(xiàn)方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2023-07-07PHP警告Cannot use a scalar value as an array的解決方法
PHP警告Cannot use a scalar value as an array的解決方法,需要的朋友可以參考下。2012-01-01如何用PHP編寫簡(jiǎn)單的api數(shù)據(jù)接口
這篇文章主要介紹了如何用PHP編寫簡(jiǎn)單的api數(shù)據(jù)接口,對(duì)數(shù)據(jù)接口感興趣的同學(xué),可以參考一下,并且親自試驗(yàn)一下2021-04-04PHP Laravel軟刪除的實(shí)現(xiàn)方法介紹
軟刪除就是邏輯刪除,數(shù)據(jù)保留單標(biāo)記上刪除狀態(tài),一般我們會(huì)用刪除時(shí)間來(lái)作為標(biāo)記,這樣標(biāo)記狀態(tài)有了,刪除時(shí)間也有了2022-09-09