⑴ 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、选择“自选封面”-然后开始播放视频,在刚开始的地方,暂停视频,然后点击下面的截...