本文目錄一覽:
如何用php調用圖片?
PHP調用?好的,如果是HTML的話很簡單(只需要img標記插入即可),那麼我回答一下PHP的載入吧,代碼如下:
// 設置圖片路徑
$path = ‘/tupian/1.jpg’;
// 獲取圖片信息
$imgInfo = getimagesize($path);
// 獲取圖片類型
$imgType = $imgInfo[2];
// 檢測圖片類型
switch ($imgType) {
case 1: // gif
// 採用gif方式載入
$img = imagecreatefromgif($path);
// 聲明文件為圖片類型
header(‘Content-Type:image/gif;’);
// 採用gif方式輸出
imagegif($img);
break;
case 2: // jpg
// 採用jpg方式載入
$img = imagecreatefromjpeg($path);
// 聲明文件為圖片類型
header(‘Content-Type:image/jpeg;’);
// 採用jpeg方式輸出
imagejpeg($img);
break;
case 3: // png
// 採用png方式載入
$img = imagecreatefrompng($path);
// 聲明文件為圖片類型
header(‘Content-Type:image/png;’);
// 採用png方式輸出
imagepng($img);
break;
default:
exit(‘圖片格式不支持!’);
}
// 銷毀圖片資源
imagedestroy($img);
// 刪除變數
unset($img);
以上若還有什麼不明白的,歡迎追問~
php怎麼讀取內容頁的圖片
一般不向資料庫插入圖片 而是插入圖片的src 通過src找到圖片然後顯示。
?php
session_start();
//array數組中放圖片的格式
$uptypes = array(“image/jpg”,”image/jpeg”,”image/png”,”image/pjpeg”,”image/gif”,”image/bmp”,”image/x-png”);
$files =$_FILES[“uppic”];
if($files[“size”]2097152){ //圖片大小判斷
echo “上傳圖片不能大於2M”;
echo “meta http-equiv=’REFRESH’ CONTENT=’1;URL=pic.php'”;
exit;
}
$ftype =$files[“type”];
if(!in_array($ftype,$uptypes)){ //圖片格式判斷
echo “上傳的圖片文件格式不正確”;
echo “meta http-equiv=’REFRESH’ CONTENT=’1;URL=pic.php'”;
}
$fname = $files[“tmp_name”]; //在伺服器臨時存儲名稱
$image_info = getimagesize($fname);
$name = $files[“name”];
$str_name = pathinfo($name); //以數組的形式返迴文件路勁的信息
$extname = strtolower($str_name[“extension”]); //把字元串改為小寫 extensiorn擴展名
$upload_dir = “upload/”; //upload文件夾
$file_name = date(“YmdHis”).rand(1000,9999).”.”.$extname;
$str_file = $upload_dir.$file_name; //文件目錄
//存入資料庫
$con=mysql_connect(“localhost”,”root”,””);
if(!$con){
die((“資料庫連接失敗”).mysql_error());
}
mysql_select_db(“mywork”,$con);
$sql=”update user set picpath=’$str_file’ where user_name=’$username'”; //將圖片地址插入資料庫mywork
mysql_query($sql,$con);
mysql_close($con);
if(!file_exists($upload_dir)){
mkdir($upload_dir); //創建目錄 成功則返回true 失敗則返回flase
}
if(!move_uploaded_file($files[“tmp_name”],$str_file)){ //將上傳的文件移動到新的目錄 要移動文件和文件新目錄 成功則返回true
echo “圖片上傳失敗”;
echo “meta http-equiv=’REFRESH’ CONTENT=’1;URL=插入失敗後希望跳轉的頁面”;
}
else{
//echo “img src=”.$str_file.””;
echo “圖片上傳成功”;
echo “meta http-equiv=’REFRESH’ CONTENT=’1;URL=插入成功希望挑戰的頁面”;
}
php怎麼獲取本地圖片信息
用glob很方便就可獲得。
$img = array(‘gif’,’png’,’jpg’);//所有圖片的後綴名
$dir = ‘data/’;//文件夾名稱
$pic = array();
foreach($img as $k=$v)
{
$pattern = $dir.’*.’.$v;
$all = glob($pattern);
$pic = array_merge($pic,$all);
}
foreach($pic as $p)
{
//分行分頁顯示代碼
}
如果和圖片是同一個文件夾,那要將
$dir = ‘data/’;//文件夾名稱
改成
$dir = ‘./’;//文件夾名稱
PHP如何讀取照片的exif信息實現代碼
先來了解什麼是圖片的Exif信息
Exif是一種圖象文件格式,它的數據存儲與JPEG格式是完全相同的。
實際上Exif格式就是在JPEG格式頭部插入了數碼照片的信息,包括拍攝時的光圈、快門、白平衡、ISO、焦距、日期時間等各種和拍攝條件以及相機品牌、型號、色彩編碼、拍攝時錄製的聲音以及全球定位系統(GPS)、縮略圖等。簡單地說,Exif=JPEG+拍攝參數。
因此,你可以利用任何可以查看JPEG文件的看圖軟體瀏覽Exif格式的照片,但並不是所有的圖形程序都能處理Exif信息。
具體代碼你可以參考下面的
?
/**
* 獲取圖象信息的函數
* 一個全面獲取圖象信息的函數
* @access public
* @param string $img 圖片路徑
* @return array
*/
function GetImageInfoVal($ImageInfo,$val_arr) {
$InfoVal = “未知”;
foreach($val_arr as $name=$val) {
if ($name==$ImageInfo) {
$InfoVal = $val;
break;
}
}
return $InfoVal;
}
function GetImageInfo($img) {
$imgtype = array(“”, “GIF”, “JPG”, “PNG”, “SWF”, “PSD”, “BMP”, “TIFF(intel byte order)”, “TIFF(motorola byte order)”, “JPC”, “JP2”, “JPX”, “JB2”, “SWC”, “IFF”, “WBMP”, “XBM”);
$Orientation = array(“”, “top left side”, “top right side”, “bottom right side”, “bottom left side”, “left side top”, “right side top”, “right side bottom”, “left side bottom”);
$ResolutionUnit = array(“”, “”, “英寸”, “厘米”);
$YCbCrPositioning = array(“”, “the center of pixel array”, “the datum point”);
$ExposureProgram = array(“未定義”, “手動”, “標準程序”, “光圈先決”, “快門先決”, “景深先決”, “運動模式”, “肖像模式”, “風景模式”);
$MeteringMode_arr = array(
“0” = “未知”,
“1” = “平均”,
“2” = “中央重點平均測光”,
“3” = “點測”,
“4” = “分區”,
“5” = “評估”,
“6” = “局部”,
“255” = “其他”
);
$Lightsource_arr = array(
“0” = “未知”,
“1” = “日光”,
“2” = “熒光燈”,
“3” = “鎢絲燈”,
“10” = “閃光燈”,
“17” = “標準燈光A”,
“18” = “標準燈光B”,
“19” = “標準燈光C”,
“20” = “D55”,
“21” = “D65”,
“22” = “D75”,
“255” = “其他”
);
$Flash_arr = array(
“0” = “flash did not fire”,
“1” = “flash fired”,
“5” = “flash fired but strobe return light not detected”,
“7” = “flash fired and strobe return light detected”,
);
$exif = exif_read_data ($img,”IFD0″);
if ($exif===false) {
$new_img_info = array (“文件信息” = “沒有圖片EXIF信息”);
}
else
{
$exif = exif_read_data ($img,0,true);
$new_img_info = array (
“文件信息” = “—————————–“,
“文件名” = $exif[FILE][FileName],
“文件類型” = $imgtype[$exif[FILE][FileType]],
“文件格式” = $exif[FILE][MimeType],
“文件大小” = $exif[FILE][FileSize],
“時間戳” = date(“Y-m-d H:i:s”,$exif[FILE][FileDateTime]),
“圖像信息” = “—————————–“,
“圖片說明” = $exif[IFD0][ImageDescription],
“製造商” = $exif[IFD0][Make],
“型號” = $exif[IFD0][Model],
“方向” = $Orientation[$exif[IFD0][Orientation]],
“水平解析度” = $exif[IFD0][XResolution].$ResolutionUnit[$exif[IFD0][ResolutionUnit]],
“垂直解析度” = $exif[IFD0][YResolution].$ResolutionUnit[$exif[IFD0][ResolutionUnit]],
“創建軟體” = $exif[IFD0][Software],
“修改時間” = $exif[IFD0][DateTime],
“作者” = $exif[IFD0][Artist],
“YCbCr位置控制” = $YCbCrPositioning[$exif[IFD0][YCbCrPositioning]],
“版權” = $exif[IFD0][Copyright],
“攝影版權” = $exif[COMPUTED][Copyright.Photographer],
“編輯版權” = $exif[COMPUTED][Copyright.Editor],
“拍攝信息” = “—————————–“,
“Exif版本” = $exif[EXIF][ExifVersion],
“FlashPix版本” = “Ver. “.number_format($exif[EXIF][FlashPixVersion]/100,2),
“拍攝時間” = $exif[EXIF][DateTimeOriginal],
“數字化時間” = $exif[EXIF][DateTimeDigitized],
“拍攝解析度高” = $exif[COMPUTED][Height],
“拍攝解析度寬” = $exif[COMPUTED][Width],
/*
The actual aperture value of lens when the image was taken.
Unit is APEX.
To convert this value to ordinary F-number(F-stop),
calculate this value’s power of root 2 (=1.4142).
For example, if the ApertureValue is ‘5’, F-number is pow(1.41425,5) = F5.6.
*/
“光圈” = $exif[EXIF][ApertureValue],
“快門速度” = $exif[EXIF][ShutterSpeedValue],
“快門光圈” = $exif[COMPUTED][ApertureFNumber],
“最大光圈值” = “F”.$exif[EXIF][MaxApertureValue],
“曝光時間” = $exif[EXIF][ExposureTime],
“F-Number” = $exif[EXIF][FNumber],
“測光模式” = GetImageInfoVal($exif[EXIF][MeteringMode],$MeteringMode_arr),
“光源” = GetImageInfoVal($exif[EXIF][LightSource], $Lightsource_arr),
“閃光燈” = GetImageInfoVal($exif[EXIF][Flash], $Flash_arr),
“曝光模式” = ($exif[EXIF][ExposureMode]==1?”手動”:”自動”),
“白平衡” = ($exif[EXIF][WhiteBalance]==1?”手動”:”自動”),
“曝光程序” = $ExposureProgram[$exif[EXIF][ExposureProgram]],
/*
Brightness of taken subject, unit is APEX. To calculate Exposure(Ev) from BrigtnessValue(Bv), you must add SensitivityValue(Sv).
Ev=Bv+Sv Sv=log((ISOSpeedRating/3.125),2)
ISO100:Sv=5, ISO200:Sv=6, ISO400:Sv=7, ISO125:Sv=5.32.
*/
“曝光補償” = $exif[EXIF][ExposureBiasValue].”EV”,
“ISO感光度” = $exif[EXIF][ISOSpeedRatings],
“分量配置” = (bin2hex($exif[EXIF][ComponentsConfiguration])==”01020300″?”YCbCr”:”RGB”),//’0x04,0x05,0x06,0x00’=”RGB” ‘0x01,0x02,0x03,0x00’=”YCbCr”
“圖像壓縮率” = $exif[EXIF][CompressedBitsPerPixel].”Bits/Pixel”,
“對焦距離” = $exif[COMPUTED][FocusDistance].”m”,
“焦距” = $exif[EXIF][FocalLength].”mm”,
“等價35mm焦距” = $exif[EXIF][FocalLengthIn35mmFilm].”mm”,
/*
Stores user comment. This tag allows to use two-byte character code or unicode. First 8 bytes describe the character code. ‘JIS’ is a Japanese character code (known as Kanji).
‘0x41,0x53,0x43,0x49,0x49,0x00,0x00,0x00’:ASCII
‘0x4a,0x49,0x53,0x00,0x00,0x00,0x00,0x00’:JIS
‘0x55,0x4e,0x49,0x43,0x4f,0x44,0x45,0x00’:Unicode
‘0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00’:Undefined
*/
“用戶注釋編碼” = $exif[COMPUTED][UserCommentEncoding],
“用戶注釋” = $exif[COMPUTED][UserComment],
“色彩空間” = ($exif[EXIF][ColorSpace]==1?”sRGB”:”Uncalibrated”),
“Exif圖像寬度” = $exif[EXIF][ExifImageLength],
“Exif圖像高度” = $exif[EXIF][ExifImageWidth],
“文件來源” = (bin2hex($exif[EXIF][FileSource])==0x03?”digital still camera”:”unknown”),
“場景類型” = (bin2hex($exif[EXIF][SceneType])==0x01?”A directly photographed image”:”unknown”),
“縮略圖文件格式” = $exif[COMPUTED][Thumbnail.FileType],
“縮略圖Mime格式” = $exif[COMPUTED][Thumbnail.MimeType]
);
}
return $new_img_info;
}
$innerhtml = “”;
$exif = GetImageInfo($_GET[‘img’]);
$innerhtml .= “TABLE”;
foreach($exif as $name=$val) {
$innerhtml .= “TRTD{$name}/TDTD{$val}/TD/TR”;
}
$innerhtml .= “TRTD colspan=\”2\””;
if ($_GET[‘img’]) {
$image = exif_thumbnail($_GET[‘img’]);
} else {
$image = false;
}
if ($image!==false) {
$innerhtml .= “img src=\”thumbnail.php?img=”.$_GET[‘img’].”\””;
} else {
// no thumbnail available, handle the error here
$innerhtml .= “No thumbnail available”;
}
$innerhtml .= “/TD/TR/TABLE”;
?
html
head
title圖片信息/title
meta http-equiv=”Content-Type” content=”text/html; charset=??????”
/head
body
SCRIPT
!–
//初始化內容值
parent.followTd?=$_GET[‘r_id’];?.innerHTML=’?=$innerhtml;?’;
//已讀取
parent.document.images.followImg?=$_GET[‘r_id’];?.loaded=’yes’;
—
/SCRIPT
/body
/html
php 讀取圖片並輸出
?php
header(‘Content-Type:image/png’);
$url = “”;//圖片鏈接
$ch = curl_init();
//Cookie:PHPSESSID=121b1127dcded8702c6a1e702c40eca4
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_COOKIE,’PHPSESSID=121b1127dcded8702c6a1e702c40eca4′);//如果不需要cookies就刪除這條語句
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT,0);//忽略超時
curl_setopt($ch, CURLOPT_NOBODY, false);
$str = curl_exec($ch);
curl_close($ch);
原創文章,作者:簡單一點,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/127407.html