⑴ php如何把上傳的照片生成高質量的縮略圖
ImageMagick沒用過,一般直接用內置的GD庫,沒有發現你說的這么嚴重的失真問題。
利用GD庫創建縮略圖的大致思路如下:
依據設定的尺寸創建真彩色畫布$im=createtruecolor(120,90);
讀取原始文件尺寸,按照原始尺寸的寬度和高度比例,計算出縮略圖的大小(可能與給定的尺寸有一定的偏差)
將原始圖像拷貝並縮放到創建的真彩色縮略圖畫布上。
輸出縮略圖文件。
可能就是因為利用的是這個真彩色,縮略圖效果還湊合,也不是說絕對不失真的。
⑵ php SWFUpload 怎麼創建縮略圖並且保存到指定文件夾裡面
php /* * swfupload圖片上傳 */ if (isset($_POST["PHPSESSID"])) { session_id($_POST["PHPSESSID"]); } session_start(); ini_set("html_errors", "0"); if (!isset($_FILES["Filedata"]) || !is_uploaded_file($_FILES["Filedata"]["tmp_name"]) ||$_FILES["Filedata"]["error"] != 0) { echo "錯誤:無效的上傳!"; exit(0); } // Get the image and create a thumbnail $file_types=explode(".",$_FILES["Filedata"]["name"]); $file_type=$file_types[count($file_types)-1]; if(strtolower($file_type)=='gif' ) { $img = imagecreatefromgif($_FILES["Filedata"]["tmp_name"]); } else if(strtolower($file_type)=='png') { $img = imagecreatefrompng($_FILES["Filedata"]["tmp_name"]); } else if(strtolower($file_type)=='bmp') { $img = imagecreatefromwbmp($_FILES["Filedata"]["tmp_name"]); } else { $img = imagecreatefromjpeg($_FILES["Filedata"]["tmp_name"]); } if (!$img) { echo "錯誤:無法創建圖像 ". $_FILES["Filedata"]["tmp_name"]; exit(0); } $width = imageSX($img); $height = imageSY($img); if (!$width || !$height) { echo "錯誤:無效的高或高"; exit(0); } // Build the thumbnail $target_width = 100; $target_height = 100; $target_ratio = $target_width / $target_height; $img_ratio = $width / $height; if ($target_ratio > $img_ratio) { $new_height = $target_height; $new_width = $img_ratio * $target_height; } else { $new_height = $target_width / $img_ratio; $new_width = $target_width; } if ($new_height > $target_height) { $new_height = $target_height; } if ($new_width > $target_width) { $new_height = $target_width; } $new_img = ImageCreateTrueColor(100, 100); if (!@imagefilledrectangle($new_img, 0, 0, $target_width-1, $target_height-1, 0)) { // Fill the image black echo "錯誤:不能填充新圖片"; exit(0); } if (!@imageresampled($new_img, $img, ($target_width-$new_width)/2, ($target_height-$new_height)/2, 0,0, $new_width, $new_height, $width, $height)) { echo "錯誤:不能調整大小的圖像"; exit(0); } if (!isset($_SESSION["file_info"])) { $_SESSION["file_info"] = array(); } ob_start(); imagejpeg($new_img); $imagevariable = ob_get_contents(); ob_end_clean(); $file_id = md5($_FILES["Filedata"]["tmp_name"] + rand()*100000); $_SESSION["file_info"][$file_id] = $imagevariable; echo "FILEID:" . $file_id; // Return the file id to the script include("upimg.class.php"); if(!empty($_FILES["Filedata"]) and count(explode(",",$_SESSION["upload_tem"]))<5) { $folder="upload/images/tem/".date("Y-m-d"); $up = new upimg("$folder","$folder"); //可以寫成:$up = new upimg(); $up->autoThumb = TRUE; //可省略 $up->srcDel=TRUE; $up->thumbWidth = 550; //可省略 $up->thumbHeight = 400; //可省略 $up->maxsize=2014; //上傳文件大小單位是kb $result= $up->upload('Filedata'); // HTML中<input />的name屬性值 $_SESSION["upload_tem"]=$_SESSION["upload_tem"].",".$up->thumbPath; $_SESSION["upload_tem"]=trim($_SESSION["upload_tem"],","); } ?>2. [代碼][PHP]代碼 生成縮略圖類upimg.class.php: <?php class upimg{ public $uploadFolder = 'upload'; // 圖片存放目錄 public $thumbFolder = 'upload/thumb'; // 縮略圖存放目錄 public $thumbWidth = ''; // 縮略圖寬度 public $thumbHeight = ''; // 縮略圖高度 public $autoThumb = ''; // 是否自動生成縮略圖 public $error = ''; // 錯誤信息 public $imgPath = ''; // 上傳成功後的圖片位置 public $thumbPath = ''; // 上傳成功後的縮略圖位置 public $maxsize=''; // 說明:初始化,創建存放目錄 function __construct($uploadFolder = 'upload', $thumbFolder = 'upload/thumb'){ $this->uploadFolder = $uploadFolder; $this->thumbFolder = $thumbFolder; $this->_mkdir(); } // 說明:上傳圖片,參數是<input />的name屬性值;成功返回圖片的相對URL,失敗返回FALSE和錯誤信息(在$this->error里) // bool/sting upload(string $html_tags_input_attrib_name); function upload($inputName){ // 上傳操作,參數是input標簽的name屬性。 if ($this->error){ // 如果有錯,直接返回(例如_mkdir) return FALSE; } if(!$_FILES[$inputName]["name"]){ $this->error = '沒有上傳圖片'; return FALSE; } //檢測文件大小 if($_FILES[$inputName]["size"] > ($this->maxsize*1024)){ $this->error = '上傳文件'.$inputName.'太大,最大支持'.ceil($this->maxsize/1024).'kb的文件'; return FALSE; } if($_FILES[$inputName]["name"]){ $isUpFile = $_FILES[$inputName]['tmp_name']; if (is_uploaded_file($isUpFile)){ $imgInfo = $this->_getinfo($isUpFile); if (FALSE == $imgInfo){ return FALSE; } $extName = $imgInfo['type']; $microSenond = floor(microtime()*10000);// 取一個毫秒級數字,4位。 $newFileName = $uploadFolder . '/' . date('YmdHis') . $microSenond . '.' . $extName ; // 所上傳圖片的新名字。 $location = $this->uploadFolder . $newFileName; $result = move_uploaded_file($isUpFile, $location); if ($result) { if (TRUE == $this->autoThumb) { // 是否生成縮略圖 $thumb = $this->thumb($location, $this->thumbWidth, $this->thumbHeight); if (FALSE == $thumb) { return FALSE; } } //是否刪除原圖 if(TRUE==$this->srcDel) { @unlink ($location); } $this->imgPath = $location; return $location; }else{ $this->error = '移動臨時文件時出錯'; return FALSE; } }else{ $uploadError = $_FILES[$inputName]['error']; if (1 == $uploadError){ // 文件大小超過了php.ini中的upload_max_filesize $this->error = '文件太大,伺服器拒絕接收大於' . ini_get('upload_max_filesize') . '的文件'; return FALSE; }elseif (3 == $uploadError){ // 上傳了部分文件 $this->error = '上傳中斷,請重試'; return FALSE; }elseif (4 == $uploadError){ $this->error = '沒有文件被上傳'; return FALSE; }elseif (6 == $uploadError){ $this->error = '找不到臨時文件夾,請聯系您的伺服器管理員'; return FALSE; }elseif (7 == $uploadError){ $this->error = '文件寫入失敗,請聯系您的伺服器管理員'; return FALSE; }else{ if (0 != $uploadError){ $this->error = '未知上傳錯誤,請聯系您的伺服器管理員'; return FALSE; } } // end if $uploadError } // end if is_uploaded_file else } // end if $_FILES[$inputName]["name"] } // 說明:獲取圖片信息,參數是上傳後的臨時文件,成功返回數組,失敗返回FALSE和錯誤信息 // array/bool _getinfo(string $upload_tmp_file) private function _getinfo($img){ if (!file_exists($img)){ $this->error = '找不到圖片,無法獲取其信息'; return FALSE; } $tempFile = @fopen($img, "rb"); $bin = @fread($tempFile, 2); //只讀2位元組 @fclose($tempFile); $strInfo = @unpack("C2chars", $bin); $typeCode = intval($strInfo['chars1'] . $strInfo['chars2']); $fileType = ''; switch ($typeCode){ // 6677:bmp 255216:jpg 7173:gif 13780:png 7790:exe 8297:rar 8075:zip tar:109121 7z:55122 gz 31139 case '255216': $fileType = 'jpg'; break; case '6677': $fileType = 'bmp'; break; case '7173': $fileType = 'gif'; break; case '13780': $fileType = 'png'; break; default: $fileType = 'unknown'; } if ($fileType == 'jpg' || $fileType == 'gif' || $fileType == 'png' || $fileType == 'bmp'){ $imageInfo = getimagesize($img); $imgInfo['size'] = $imageInfo['bits']; $imgInfo["type"] = $fileType; $imgInfo["width"] = $imageInfo[0]; $imgInfo["height"] = $imageInfo[1]; return $imgInfo; }else{ // 非圖片類文件信息 $this->error = '圖片類型錯誤'; return FALSE; } } // end _getinfo // 說明:生成縮略圖,等比例縮放或拉伸 // bool/string thumb(string $uploaded_file, int $thumbWidth, int $thumbHeight, string $thumbTail); function thumb($img, $thumbWidth = 300, $thumbHeight = 200,$thumbTail = '_thumb') { $filename = $img; // 保留一個名字供新的縮略圖名字使用 $imgInfo = $this->_getinfo($img,$i); if(FALSE == $imgInfo) { return FALSE; } $imgType = $imgInfo['type']; switch ($imgType) { // 創建一個圖,並給出擴展名 case "jpg" : $img = imagecreatefromjpeg($img); $extName = 'jpg'; break; case 'gif' : $img = imagecreatefromgif($img); $extName = 'gif'; break; case 'bmp' : $img = imagecreatefromgif($img); $extName = 'bmp'; break; case 'png' : $img = imagecreatefrompng($img); $extName = 'png'; break; default : // 如果類型錯誤,生成一張空白圖 $img = imagecreate($thumbWidth,$thumbHeight); imagecolorallocate($img,0x00,0x00,0x00); $extName = 'jpg'; } // 縮放後的圖片尺寸(小則拉伸,大就縮放) $imgWidth = $imgInfo['width']; $imgHeight = $imgInfo['height']; if($imgHeight > $imgWidth) { // 豎圖 $newHeight = $thumbHeight; $newWidth = ceil($imgWidth / ($imgHeight / $thumbHeight )); } else if($imgHeight < $imgWidth) { // 橫圖 $newHeight = ceil($imgHeight / ($imgWidth / $thumbWidth )); $newWidth = $thumbWidth; } else if($imgHeight == $imgWidth) { // 等比例圖 $newHeight = $thumbWidth; $newWidth = $thumbWidth; } $bgimg = imagecreatetruecolor($newWidth,$newHeight); $bg = imagecolorallocate($bgimg,0x00,0x00,0x00); imagefill($bgimg,0,0,$bg); $sampled = imageresampled($bgimg,$img,0,0,0,0,$newWidth,$newHeight,$imgWidth,$imgHeight); if(!$sampled ) { $this->error = '縮略圖生成失敗'; $this->path=$this->uploadFolder . '/' . $filename; return FALSE; } $filename = basename($filename); $newFileName = substr($filename, 0, strrpos($filename, ".")) . $thumbTail . '.' . $extName ; // 新名字 $thumbPath = $this->thumbFolder . '/' . $newFileName; switch ($extName){ case 'jpg': $result = imagejpeg($bgimg, $thumbPath); break; case 'gif': $result = imagegif($bgimg, $thumbPath); break; case 'png': $result = imagepng($bgimg, $thumbPath); break; default: // 上邊判斷類型出錯時會創建一張空白圖,並給出擴展名為jpg $result = imagejpeg($bgimg, $thumbPath); } if ($result) { $this->thumbPath = $thumbPath; $this->path=$this->uploadFolder . '/' . $filename; return $thumbPath; } else { $this->error = '縮略圖創建失敗'; $this->path=$this->uploadFolder . '/' . $filename; return FALSE; } } // end thumb // 說明:創建圖片的存放目錄 private function _mkdir() { // 創建圖片上傳目錄和縮略圖目錄 if(!is_dir($this->uploadFolder)) { $dir = explode('/', $this->uploadFolder); foreach($dir as $v) { if($v) { $d .= $v . '/'; if(!is_dir($d)) { $state = mkdir($d); if(!$state) { $this->error = '在創建目錄' . $d . '時出錯!'; } } } } } if(!is_dir($this->thumbFolder) && TRUE == $this->autoThumb) { $dir = explode('/', $this->thumbFolder); foreach($dir as $v) { if($v) { $d .= $v . '/'; if(!is_dir($d)) { $state = mkdir($d); if(!$state) { $this->error = '在創建目錄' . $d . '時出錯!'; } } } } } } } ?>
⑶ 求如何用php讀取指定文件夾中的所有圖片,生成縮略圖,在網頁上分頁顯示,單擊縮略圖就在新頁面顯示大圖。
生成縮略圖採用讀取文件夾的方式
$handle = opendir($dir)
while(false !== ($file = readdir($handle)))
{
if($file 是圖片)
{
生成縮略圖代碼
}
}
⑷ 如何用PHP獲取主流視頻網站的縮略圖
給你說下大體的思路吧,正好也剛寫完個爬蟲。
首先,要爬取想要的視頻頁面,先要有能用代碼模擬出來整個登錄過程,(有些需要登陸後才能顯示的),這些包括以什麼形式訪問此頁面GET?POST?,當然這里POST需要加參數才能訪問,另外,最好還要把COOIKES值設置成固定的,這樣訪問才不容易被識別為異常訪問:(我用的是官方發行的一個類-Quest.php)
->實例化HTTP_ HTTP_Request("域名");
->聲明相應的請求;$req->setMethod(HTTP_REQUEST_METHOD_GET); $req->setMethod(HTTP_REQUEST_METHOD_POST);
->發送連接;$req->setURL("http://www..com");
->執行請求:$req->sendRequest();
->得到cookies:$cookies = $req->getResponseCookies();(傳給一個新的數組,在需要反復爬取的頁面盡量傳此cookies,需要登陸爬取的必反)
->清除post和cookies:$req->clearPostData(); $req->clearCookies(); (循環爬取時需要清除)
->添加post方法:$req->addPostData(name,value,false); 參數name,參數value;
->得到理想頁:$response = $req->getResponseBody();
->得到頭信息:$resHeader = $req->getResponseHeader();
->打開指定文件:$res = fopen("c:/love/forever.txt", 'w');
->寫入 :fwrite($res,$response); #寫入
->關閉指針:fclose($res);
我爬取的去文字信息,你要爬取對應視頻圖片直接抓取出來就行了--
你可以試下這個代碼,js:在隨便網頁地址欄輸入---javascript:Ai7Mg6P='';for%20(i7M1bQz=0;i7M1bQz<document.images.length;i7M1bQz++)
{Ai7Mg6P+='<img%20src='+document.images[i7M1bQz].src+'><br>'};if(Ai7Mg6P!=''){document.write('<center>'+Ai7Mg6P+'</center>');
void(document.close())}else{alert('No%20images!')}
加油!
⑸ 如何用PHP獲取優酷、土豆、酷6、56等視頻網站的視頻縮略圖
方案1,
上那些網站,查看源代碼,從中找出那些視頻的鏈接地址,加到自己網頁里,
方案2
用PHP里的小偷工具
自己看著辦吧,你給的分也太少了
⑹ php怎麼上傳視頻時同時生成一張縮略圖
這個比較麻煩了。
涉及到幾個問題,伺服器系統+預覽插件+圖片處理,
最主要的是預覽插件
⑺ php怎樣截取視頻圖
用ffmpeg直接讀取網站的某個視頻,然後截取其中的某幀作為該視頻的縮略圖;讀取網站自身提供的視頻縮略圖。
獲取圖片路徑:
function get_youku_thumb($url) {
$content = file_get_contents($url);
preg_match( '/id="s_msn2".*?screenshot=(.*?)".?target=/', $content, $matchs );
return $matchs[1];
}
echo get_youku_thumb('視頻網址');
把過去的圖片WordPress的縮略圖,可以將獲取縮略圖的代碼做成shortcode,直接在文章中調用。也可以通過custom_field方式記錄視頻地址,在主循環中調用該函數獲得縮略圖,藉助timthumb.php等腳本生成緩存存放到本地,就不用每次都去讀網頁了。
⑻ php按百分比生成縮略圖的代碼分享
於是翻了一下手冊,弄懂幾個函數後自己寫了一個簡單的php生成縮略圖的程序。沒有用類,我覺得一個函數就能搞定,而且對於新手來說更容易去理解,從而可以幫助到更多的人。
支持按比分比縮略,支持按指定的長寬縮略,默認按百分比。程序中注釋已經很詳細了,如有問題可在下面留言,歡迎與我交流。
源碼如下:
復制代碼
代碼如下:
<?php
/*
*
param
ori_img
原圖像的名稱和路徑
*
param
new_img
生成圖像的名稱
*
param
percent
表示按照原圖的百分比進行縮略,此項為空時默認按50%
*
param
width
指定縮略後的寬度
*
param
height
指定縮略後的高度
*
*
註:當
percent
width
height
都傳入值的時候,且percent>0時,優先按照百分比進行縮略
*
by:http://www.jb51.net
更多源碼與你分享
*
溫馨提示:使用此功能要在php.ini中開啟
gd2
*
**/
function
makeThumb($ori_img,
$new_img,
$percent=50,
$width=0,
$height=0){
$original
=
getimagesize($ori_img); //得到圖片的信息,可以print_r($original)發現它就是一個數組
//$original[2]是圖片類型,其中1表示gif、2表示jpg、3表示png
switch($original[2]){
case
1
:
$s_original
=
imagecreatefromgif($ori_img);
break;
case
2
:
$s_original
=
imagecreatefromjpeg($ori_img);
break;
case
3
:
$s_original
=
imagecreatefrompng($ori_img);
break;
}
if($percent
>
0){
$width
=
$original[0]
*
$percent
/
100;
$width
=
($width
>
0)
?
$width
:
1;
$height
=
$original[1]
*
$percent
/
100;
$height
=
($height
>
0)
?
$height
:
1;
}
//創建一個真彩的畫布
$canvas
=
imagecreatetruecolor($width,$height);
imageresized($canvas,
$s_original,
0,
0,
0,
0,
$width,
$height,
$original[0],
$original[1]);
//header("Content-type:image/jpeg");
//imagejpeg($canvas); //向瀏覽器輸出圖片
$loop
=
imagejpeg($canvas,
$new_img); //生成新的圖片
if($loop){
echo
"OK!<br/>";
}
}
makeThumb("bhsj.jpg","suolue1.jpg",15,0,0); //生成原圖15%的縮略圖
makeThumb("bhsj.jpg","suolue2.jpg",0,200,120); //生成寬為100px,高為60px的縮略圖
makeThumb("bhsj.jpg","suolue3.jpg",15,200,120); //生成原圖15%的縮略圖(參數都填時,百分率優先順序大)
?>
⑼ PHP批量生成縮略圖
用不著那麼麻煩,直接下載文件夾,用光影魔術手,批量生成文件夾內的文件,同名覆蓋上去就了,連水印,效果,邊框什麼的都可以加上去,方便省時;
如果硬要寫程序的話,可以用「jsw0523」裡面寫的img2thumb 的函數,通過資料庫讀取數據循環,直接循環體內通過參數調用就行了
⑽ php截取視頻第一幀 作為略縮圖,怎麼弄
你的這個需求是能實現的,事先的原理是:視頻網站的自定義視頻縮略圖功能,在這里以優酷視頻為例說明: 1、登錄賬號,進入到個人視頻中心,點擊視頻後面的編輯 2、選擇「自選封面」-然後開始播放視頻,在剛開始的地方,暫停視頻,然後點擊下面的截...