溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

php處理圖片不同尺寸顯示的方法

發(fā)布時(shí)間:2020-07-03 16:27:22 來(lái)源:億速云 閱讀:346 作者:Leah 欄目:編程語(yǔ)言

這篇文章運(yùn)用簡(jiǎn)單易懂的例子給大家介紹php處理圖片不同尺寸顯示的方法,代碼非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

一張圖片可能會(huì)在不同的地方顯示,大小不同,比例也不同,因此本例介紹的這個(gè)圖片自動(dòng)裁切還是比較有用的,有需求的朋友可以看看

php處理圖片不同尺寸顯示的方法

如果做過(guò)那種門(mén)戶站的朋友,肯定知道,一張圖片可能會(huì)在不同的地方顯示、大小不同、比例也不同,如果只用一張圖的話,那么肯定會(huì)變形,而且在顯示小圖的地方,鏈接大圖,又太浪費(fèi)了.....用縮略圖來(lái)處理,也不完美,因?yàn)槊總€(gè)地方出現(xiàn)的比例大小可能都不一樣 。

PHP自動(dòng)裁切相比你們看到過(guò)類似那種圖片地址/aaaa/abc_200_100.jpg 或者/aaaa/abc_200*100.jpg

我的方式就是在需要圖片地方把這個(gè)圖片地址轉(zhuǎn)化為類似上面的那種地址, 然后通過(guò)apache 的rewrite 定向到一個(gè)處理程序。根據(jù)寬高生成一個(gè)圖片然后保存起來(lái),在不動(dòng)原圖的任何信息和位置的情況下對(duì)圖片做處理。

不好的地方,就是生成的圖片可能會(huì)比較多,占用的空間也比較大,但是如果是自己服務(wù)器 那就無(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('原圖地址','寬度','高度'); 這個(gè)函數(shù)返回處理過(guò)的圖片地址,img 函數(shù)直接返回圖片標(biāo)簽字符串,比如在discuz模板里面調(diào)用這個(gè)函數(shù) {eval img($pic,200,100)}

這樣返回的地址就是/data/attachment/forum/aaaaaa_200_100.jpg 目前來(lái)說(shuō) 這個(gè)圖片是不存在 那么看第二步

第二步 需要添加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/這個(gè)地址開(kāi)頭不存在的文件都定向到image.php來(lái)處理,并且把url當(dāng)參數(shù)傳過(guò)去

第三步 就是image.php 這個(gè)里面的代碼里

<?php

$url = $_GET['url'];
$src = './data/attachment/' . preg_replace('/_(\d+)_(\d+)/', '', $url);
$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();
}

thumb 函數(shù) 可以控制 裁切方式,scale 為等比縮放,不裁切,不夠的地方 用白色填充,crop 為裁切,如果要求的寬高比 大于原圖寬高比,那么就保持最大顯示寬度,居中裁切上下多余部分,如果要求寬高比小于原圖寬高比,那么就保持最大高度,居中裁切左右多余部分,總而言之,在保持不變形的前提下 ,把圖片縮小,而且最大保留圖片的內(nèi)容.哈哈 這個(gè)代碼有多叼,試試知道了,,,當(dāng)然你需要支持rewrite功能和GD2 支持

關(guān)于php處理圖片不同尺寸顯示的方法就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI