當前位置:首頁 » 編程語言 » php判斷圖片大小

php判斷圖片大小

發布時間: 2025-05-22 16:37:13

① 求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圖片處理中如何對圖片進行壓縮並保持不失真的函數說明。

② php怎麼實現根據圖片搜索圖片功能

php愛好者 們很對php開發的追求是永不止步的,今天偶然想起來了 發下
php怎麼實現相似圖片的搜索呢?
其中的原理來解釋下
1、縮小尺寸。將圖片縮小到8×8的尺寸,總共64個像素。這一步的作用是去除圖片的細節,只保留結構、明暗等基本信息,摒棄不同尺寸、比例帶來的圖片差異。
2、簡化色彩。將縮小後的圖片,轉為64級灰度。也就是說,所有像素點總共只有64種顏色。
3、計算平均值。計算所有64個像素的灰度平均值。
4、比較像素的灰度。將每個像素的灰度,與平均值進行比較。大於或等於平均值,記為1;小於平均值,記為0。
5、計算哈希值。將上一步的比較結果,組合在一起,就構成了一個64位的整數,這就是這張圖片的指紋。組合的次序並不重要,只要保證所有圖片都採用同樣次序就行了。得到指紋以後,就可以對比不同的圖片,看看64位中有多少位是不一樣的。
使用代碼
hash($f);
}
return $isString ? $result[0] : $result;
}
public function checkIsSimilarImg($imgHash, $otherImgHash){
if (file_exists($imgHash) && file_exists($otherImgHash)){
$imgHash = $this->run($imgHash);
$otherImgHash = $this->run($otherImgHash);
}
if (strlen($imgHash) !== strlen($otherImgHash)) return false;
$count = 0;
$len = strlen($imgHash);
for($i=0;$i<$len;$i++){
if ($imgHash{$i} !== $otherImgHash{$i}){
$count++;
}
}
return $count <= (5 * $rate * $rate) ? true : false;
}
public function hash($file){
if (!file_exists($file)){
return false;
}
$height = 8 * $this->rate;
$width = 8 * $this->rate;
$img = imagecreatetruecolor($width, $height);
list($w, $h) = getimagesize($file);
$source = $this->createImg($file);
imageresampled($img, $source, 0, 0, 0, 0, $width, $height, $w, $h);
$value = $this->getHashValue($img);
imagedestroy($img);
return $value;
}
public function getHashValue($img){
$width = imagesx($img);
$height = imagesy($img);
$total = 0;
$array = array();
for ($y=0;$y<$height;$y++){
for ($x=0;$x<$width;$x++){
$gray = ( imagecolorat($img, $x, $y) >> 8 ) & 0xFF;
if (!is_array($array[$y])){
$array[$y] = array();
}
$array[$y][$x] = $gray;
$total += $gray;
}
}
$average = intval($total / (64 * $this->rate * $this->rate));
$result = '';
for ($y=0;$y<$height;$y++){
for ($x=0;$x<$width;$x++){
if ($array[$y][$x] >= $average){
$result .= '1';
}else{
$result .= '0';
}
}
}
return $result;
}
public function createImg($file){
$ext = $this->getFileExt($file);
if ($ext === 'jpeg') $ext = 'jpg';
$img = null;
switch ($ext){
case 'png' : $img = imagecreatefrompng($file);break;
case 'jpg' : $img = imagecreatefromjpeg($file);break;
case 'gif' : $img = imagecreatefromgif($file);
}
return $img;
}
public function getFileExt($file){
$infos = explode('.', $file);
$ext = strtolower($infos[count($infos) - 1]);
return $ext;
}
}
調用方式如下:
require_once "Imghash.class.php";
$instance = ImgHash::getInstance();
$result = $instance->checkIsSimilarImg('chenyin/IMG_3214.png', 'chenyin/IMG_3212.JPG');
如果$result值為true, 則表明2個圖片相似,否則不相似。

③ 各位php里怎麼獲取base64的圖片大小啊

$size=file_get_contents('base64字元串');
print_r(strlen($size)/1024);

④ php如何限制圖片上傳的長和寬

$size = getimagesize(上傳圖片的臨時路徑);
$width = $size[0];

$height = $size[1];
if($width>200 || $height>200){
echo "圖片長或寬超出限制";
exit;
}

⑤ php 在多個上傳圖片時 怎麼獲取 要上傳的圖片寬高

分兩個部分,一個是「多個圖片」,一個是獲取圖片的寬、高。
第一個用循環做到,也就是用for、foreach之類的東西遍歷你從頁面中得到的圖片數組
下一步用getimagesize()函數獲得圖片的信息。
此函數在php手冊里有解釋:圖像生成與處理→GD→GD and image函數里。
此函數返回的結果如下:
返回一個具有四個單元的數組。索引 0 包含圖像寬度的像素值,索引 1 包含圖像高度的像素值。索引 2 是圖像類型的標記:1 = GIF,2 = JPG,3 = PNG,4 = SWF,5 = PSD,6 = BMP,7 = TIFF(intel byte order),8 = TIFF(motorola byte order),9 = JPC,10 = JP2,11 = JPX,12 = JB2,13 = SWC,14 = IFF,15 = WBMP,16 = XBM。這些標記與 PHP 4.3.0 新加的 IMAGETYPE 常量對應。索引 3 是文本字元串,內容為「height="yyy" width="xxx"」,可直接用於 IMG 標記。

⑥ php中如何調用資料庫中的圖片並且顯示到頁面

php是採用二進制形式存儲圖片及讀取顯示的,首先通過代碼創建數據表,然後上傳圖片伺服器再通過瀏覽器顯示,具體編程代碼舉例:

1、首先需要創建數據表,具體代碼如下圖所示。

⑦ php開發需要獲取圖片的解析度 DPI

getimagesize 這個函數就是獲取width和height

⑧ php獲取圖片解析度 顏色模式函數

$img_info = getimagesize('a.jpg');
print_r($img_info);

索引 0 包含圖像寬度的像素值,索引 1 包含圖像高度的像素值。索引 2 是圖像類型的標記:1 = GIF,2 = JPG,3 = PNG,4 = SWF,5 = PSD,6 = BMP,7 = TIFF(intel byte order),8 = TIFF(motorola byte order),9 = JPC,10 = JP2,11 = JPX,12 = JB2,13 = SWC,14 = IFF,15 = WBMP,16 = XBM。

channels 和 bits。channels 對於 RGB 圖像其值為 3,對於 CMYK 圖像其值為 4。bits 是每種顏色的位數。

熱點內容
尿素脫硝存儲罐 發布:2025-05-22 22:28:20 瀏覽:344
伺服器系統卡壞了怎麼辦 發布:2025-05-22 22:26:45 瀏覽:375
存儲卡密碼 發布:2025-05-22 22:20:20 瀏覽:846
java文件傳輸 發布:2025-05-22 22:18:35 瀏覽:401
伺服器如何搭建掛機 發布:2025-05-22 22:05:52 瀏覽:62
壓縮毛巾圖片 發布:2025-05-22 22:04:59 瀏覽:747
蘋果筆記本電腦怎麼登錄雲伺服器 發布:2025-05-22 22:04:01 瀏覽:629
解壓文件導入excel 發布:2025-05-22 21:54:32 瀏覽:6
安卓手機為什麼總卡返回屏幕 發布:2025-05-22 21:40:52 瀏覽:450
不知道開機密碼怎麼辦 發布:2025-05-22 21:39:08 瀏覽:511