PHP圖片自動裁切應(yīng)付不同尺寸的顯示_PHP教程
推薦:PHP 抽象方法與抽象類abstract關(guān)鍵字介紹及應(yīng)用PHP 抽象方法與抽象類 abstract 關(guān)鍵字 abstract 關(guān)鍵字用于定義抽象方法與抽象類。 抽象方法 抽象方法指沒有方法體的方法,具體就是在方法聲明的時候沒有 {} 括弧以及其中的內(nèi)容,而是直接在聲明時在方法名后加上分號結(jié)束。 abstract 關(guān)鍵字用于定義抽象方法,語法:
如果做過那種門戶站的朋友,肯定知道,一張圖片可能會在不同的地方顯示,大小不同,比例也不同,
如果只用一張圖的話,那么肯定會變形,而且在顯示小圖的地方,鏈接 大圖,又太浪費(fèi)了.....用縮略圖來處理,也不完美,因?yàn)槊總地方出現(xiàn)的比例 大小可能都不一樣 ,舉個例子!
請看上圖。
在這個地方,其實(shí)調(diào)去出來的是一個列表,但是 圖片的大小是不一樣的,有多大寬有的窄,,當(dāng)遇到這樣的情況的時候 你們怎么辦呢,如果直接用原來的地址,肯定是會變形的,如果搞縮略圖也不靠譜,這個調(diào)去是自動調(diào)去的,你根本不知道哪個圖片需要多大的寬高,
------------------------------------------------------------------------------------------------------------------
下面進(jìn)入正題:
我一直用一種方法,就是PHP 自動裁切...相比你們看到過類似那種圖片地址吧 /aaaa/abc_200_100.jpg 或者/aaaa/abc_200*100.jpg
我的方式就是在需要圖片地方把這個圖片地址轉(zhuǎn)化為 類似上面的那種地址, 然后通過apache 的rewrite 定向到一個處理程序.根據(jù)寬高生成一個圖片然后保存起來,
這樣做的好處有幾個地方:
第一,非常靈活,在有圖片地方,你需要多寬多高,都可以隨意 控制,不會變形,而且程序永遠(yuǎn)會讓圖片內(nèi)容顯示的最多
第二個,當(dāng)圖片生成過一次的時候,apache下次就不會再重定向到程序了, 因?yàn)樵谝?guī)則前面 有 !d !f 這個判斷,意思就是當(dāng)前文件不存在的時候才會定向走,下次圖片存在了,就不會再出來了 直接就是真是的圖片了
不好的地方,就是生成的圖片可能會比較多,占用的空間也比較大,但是如果是自己服務(wù)器 那就無所謂了,可以歸類整理下
OK 奉上代碼,我們就以discuz為例
function crop_img($img, $width = 200, $height = 200) {
$img_info = parse_url($img);
/* 外部鏈接直接返回圖片地址 */
if (!empty($img_info['host']) && $img_info['host'] != $_SERVER['HTTP_HOST']) {
return $img;
} else {
$pos = strrpos($img, '.');
$img = substr($img, 0, $pos) . '_' . $width . '_' . $height . substr($img, $pos);
return $img;
}
}
function img($img,$width,$height){
$img_info = parse_url($img);
/* 外部鏈接直接返回圖片地址 */
if (!empty($img_info['host']) && $img_info['host'] != $_SERVER['HTTP_HOST']) {
return $img;
} else {
$pos = strrpos($img, '.');
$img = substr($img, 0, $pos) . '_' . $width . '_' . $height . substr($img, $pos);
echo '<img src="'.$img.'" width="'.$width.'" height="'.$height.'" />';
return ;
}
}
函數(shù)的用法 crop_img('原圖地址','寬度','高度'); 這個函數(shù)返回處理過的圖片地址,img 函數(shù)直接返回圖片標(biāo)簽字符串,比如在discuz模板里面調(diào)用這個函數(shù) {eval img($pic,200,100)}
這樣返回的地址就是/data/attachment/forum/aaaaaa_200_100.jpg 目前來說 這個圖片是不存在 那么看第二步
第二步 需要添加apache的rewrite規(guī)則
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^data/attachment/(.*)$ images.php?url=$1 [L]
</IfModule>
上面的意思,就是 data/attachement/這個地址開頭不存在的文件都定向到image.php來處理,并且把url當(dāng)參數(shù)傳過去
第三部就是image.php 這個里面的代碼里
<?php
$url = $_GET['url'];
$src = './data/attachment/' . $filename = './data/attachment/' . $url;
if (file_exists($filename)) {
ob_start();
header('Content-type:image/jpeg');
readfile($filename);
ob_flush();
flush();
} else {
if(!preg_match('/_(\d+)_(\d+)/', $url, $wh)){
defulat();
exit();
}
$width = $wh[1];
$height = $wh[2];
thumb(realpath($src), $width, $height, $filename, 'crop', '85');
}
function thumb($src, $width, $height, $filename, $mode = 'scale', $quality = '100') {
try {
$imageValue = getimagesize($src);
$sourceWidth = $imageValue[0]; //原圖寬
$sourceHeight = $imageValue[1]; //原圖高
$thumbWidth = $width; //縮略圖寬
$thumbHeight = $height; //縮略圖高
$_x = 0;
$_y = 0;
$w = $sourceWidth;
$h = $sourceHeight;
if ($mode == 'scale') {
if ($sourceWidth <= $thumbWidth && $sourceHeight <= $thumbHeight) {
$_x = floor(($thumbWidth - $sourceWidth) / 2);
$_y = floor(($thumbHeight - $sourceHeight) / 2);
$thumbWidth = $sourceWidth;
$thumbHeight = $sourceHeight;
} else {
if ($thumbHeight * $sourceWidth > $thumbWidth * $sourceHeight) {
$thumbHeight = floor($sourceHeight * $width / $sourceWidth);
$_y = floor(($height - $thumbHeight) / 2);
} else {
$thumbWidth = floor($sourceWidth * $height / $sourceHeight);
$_x = floor(($width - $thumbWidth) / 2);
}
}
} else if ($mode == 'crop') {
if ($sourceHeight < $thumbHeight) { //如果原圖尺寸小于當(dāng)前尺寸
$thumbWidth = floor($thumbWidth * $sourceHeight / $thumbHeight);
$thumbHeight = $sourceHeight;
}
if ($sourceWidth < $thumbWidth) {
$thumbHeight = floor($thumbHeight * $sourceWidth / $thumbWidth);
$thumbWidth = $sourceWidth;
}
$s1 = $sourceWidth / $sourceHeight; //原圖比例
$s2 = $width / $height; //新圖比例
if ($s1 == $s2) {
} else if ($s1 > $s2) { //全高度
$y = 0;
$ax = floor($sourceWidth * ($thumbHeight / $sourceHeight));
$x = ($ax - $thumbWidth) / 2;
$w = $thumbWidth / ($thumbHeight / $sourceHeight);
} else { //全寬度
$x = 0;
$ay = floor($sourceHeight * ($thumbWidth / $sourceWidth)); //模擬原圖比例高度
$y = ($ay - $thumbHeight) / 2;
$h = $thumbHeight / ($thumbWidth / $sourceWidth);
}
}
switch ($imageValue[2]) {
case 2: $source = imagecreatefromjpeg($src);
break;
case 1: $source = imagecreatefromgif($src);
break;
case 3: $source = imagecreatefrompng($src);
break;
case 6: $source = imagecreatefromwbmp($src);
break;
default: defulat();
return;
}
header("Content-type: image/jpeg");
$thumb = imagecreatetruecolor($width, $height);
imagefill($thumb, 0, 0, imagecolorallocate($thumb, 255, 255, 255));
imagecopyresampled($thumb, $source, 0, 0, $x, $y, $width, $height, $w, $h);
imagejpeg($thumb, null, $quality);
// if ($_SERVER['HTTP_REFERER'] || false !== stripos($_SERVER['HTTP_REFERER'], 'http://' . $_SERVER['SERVER_NAME'])) {
imagejpeg($thumb, $filename, $quality);
// }
imagedestroy($thumb);
imagedestroy($source);
} catch (Exception $ex) {
defulat();
}
}
function defulat() {
$default_img = realpath('media/images/nopic.jpg');
ob_start();
header('Content-type:image/jpeg');
readfile($default_img);
ob_flush();
flush();
}
分享:PHP編碼轉(zhuǎn)換函數(shù)mb_convert_encoding與iconv用法將一個短信接口代碼從apache遷移到nginx+php-fpm后,發(fā)現(xiàn)無法發(fā)出短信了,查看php日志, [25-Sep-2014 20:15:21] WARNING: [pool www] child 9617 said into stderr: NOTICE: PHP message: PHP Fatal error: Call to undefined function mb_convert_encoding() in /dat
- PHP 抽象方法與抽象類abstract關(guān)鍵字介紹及應(yīng)用
- PHP編碼轉(zhuǎn)換函數(shù)mb_convert_encoding與iconv用法
- SAE的storage服務(wù)使用方法
- php輸出控制的學(xué)習(xí)筆記
- Python簡單技巧和常用參考
- 使用Poco C++庫創(chuàng)建websocket安全訪問(wss)客戶端
- php刪除指定目錄下的相關(guān)文件實(shí)例
- PHP流程控制的替代語法示例
- PHP的三種類型的運(yùn)算符
- php實(shí)現(xiàn)mysql數(shù)據(jù)庫隨機(jī)重排例子
- PHP如何判斷一個gif圖片是否為動態(tài)圖片
- php 一句話刪除目錄下所有文件
PHP教程Rss訂閱編程教程搜索
PHP教程推薦
- 《PHP設(shè)計(jì)模式介紹》第十八章 結(jié)論
- 談mySQL數(shù)據(jù)庫的UTF8中文網(wǎng)站全文檢索的實(shí)現(xiàn)
- php上傳apk后自動提取apk包信息的使用(示例下載)
- 使用php 5時MySQL返回亂碼的解決辦法
- php啟動時候提示PHP startup的解決方法
- PHP將漢字轉(zhuǎn)換拼音
- 基于flush()不能按順序輸出時的解決辦法
- PHP分頁效率終結(jié)版(推薦)
- 如何才能將數(shù)據(jù)從文本導(dǎo)入到mysql數(shù)據(jù)庫
- php面向?qū)ο髉ublic private protected 訪問修飾符
- 相關(guān)鏈接:
- 教程說明:
PHP教程-PHP圖片自動裁切應(yīng)付不同尺寸的顯示
。
