PHP 自动缩放函数并在HTML<img>标签中可以输出 如题.图片尺寸长大于800px时.如何调用GD2函数.将图片按比例缩小.并且可以被HTML<img>调用? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 //缩放图片function resize($srcImage,$toFile,$maxWidth = 100,$maxHeight = 100,$imgQuality=100){ list($width, $height, $type, $attr) = getimagesize($srcImage); switch ($type) { case 1: $img = imagecreatefromgif($srcImage); break; case 2: $img = imagecreatefromjpeg($srcImage); break; case 3: $img = imagecreatefrompng($srcImage); break; } $scale = min($maxWidth/$width, $maxHeight/$height); //求出绽放比例 if($scale < 1) { $newWidth = floor($scale*$width); $newHeight = floor($scale*$height); $newImg = imagecreatetruecolor($newWidth, $newHeight); imagecopyresampled($newImg, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height); $newName = ""; switch($type) { case 1: if(imagegif($newImg, "$toFile$newName.gif", $imgQuality)) return "$newName.gif"; break; case 2: if(imagejpeg($newImg, "$toFile$newName.jpg", $imgQuality)) return "$newName.jpg"; break; case 3: if(imagepng($newImg, "$toFile$newName.png", $imgQuality)) return "$newName.png"; break; default: if(imagejpeg($newImg, "$toFile$newName.jpg", $imgQuality)) return "$newName.jpg"; break; } imagedestroy($newImg); } imagedestroy($img); return false;}//直接压制指定大小,不进行等比处理function resize_2($srcImage,$toFile,$maxWidth = 100,$maxHeight = 100,$imgQuality=100){ list($width, $height, $type, $attr) = getimagesize($srcImage); switch ($type) { case 1: $img = imagecreatefromgif($srcImage); break; case 2: $img = imagecreatefromjpeg($srcImage); break; case 3: $img = imagecreatefrompng($srcImage); break; } $scale = min($maxWidth/$width, $maxHeight/$height); //求出绽放比例 $newWidth = $maxWidth; $newHeight = $maxHeight; $newImg = imagecreatetruecolor($newWidth, $newHeight); imagecopyresampled($newImg, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height); $newName = ""; switch($type) { case 1: if(imagegif($newImg, "$toFile$newName.gif", $imgQuality)) return "$newName.gif"; break; case 2: if(imagejpeg($newImg, "$toFile$newName.jpg", $imgQuality)) return "$newName.jpg"; break; case 3: if(imagepng($newImg, "$toFile$newName.png", $imgQuality)) return "$newName.png"; break; default: if(imagejpeg($newImg, "$toFile$newName.jpg", $imgQuality)) return "$newName.jpg"; break; imagedestroy($newImg); } imagedestroy($img); return false; }//裁剪图片function cutImg($o_file,$x1,$x2,$y1,$y2){ //设置文件参数 //$o_file="./uploadfiles/200902/13/1234515154_QcA.jpg";//原文件文件路径 $file=basename($o_file);//文件 $ext=end(explode(".", $file));//扩展名 $filename=basename($file,$ext);//文件名 $filelen=strlen($file); $path=substr($o_file,0,strlen(($o_file))-$filelen);//文件夹 $newfile=$path."edit_".$file;//新文件名 $newthufile=$path."thumb_".$file;//新文件名 header('Content-type: image/jpeg'); list($width, $height) = getimagesize($o_file); $new_width = $x2-$x1; $new_height = $y2-$y1; $image_n = imagecreatetruecolor($new_width, $new_height); $image = imagecreatefromjpeg($o_file); imagecopyresampled($image_n, $image, 0, 0, $x1, $y1, $new_width, $new_height, $new_width, $new_height); //输出文件 imagejpeg($image_n, $newfile, 100); $newfileName = $newfile; return $newfile;}利用上面的函数将指定大小的图片按指定路径生成出来。再进行调用。 http://hi.baidu.com/bit5566/blog/item/80083502742486054afb51ea.htmlhttp://www.google.com.hk/search?hl=zh-CN&newwindow=1&safe=strict&client=aff-cs-360se&hs=CxX&q=php+%E7%BC%A9%E6%94%BE%E5%9B%BE%E7%89%87%E5%87%BD%E6%95%B0&meta=&aq=f&aqi=&aql=&oq=&gs_rfai= queryphp框架自带无损缩放,无压扁剪切(就是缩放到指定大小,而图像不会被压扁或拉伸了),可以定动剪切头像。http://code.google.com/p/queryphp/downloads/list php逻辑问题 新手session 够物车 不能显示以买物品 php4配置问题,各位高手帮帮忙! 求星期 在同一文件中怎么实现提交多次?在线等。。 截取汉字出现乱码的问题 急,在线等 大家在用php+mysql做blog的时候,是怎样生成rss(xml文件)的啊? 搜索学多科学员的SQL语句怎么写? Ubuntu下新建立的PHP环境出现403 请教一个关于表单复选框的问题. 继承过UCenter1.5 的进啊 有几个简单的问题
function resize($srcImage,$toFile,$maxWidth = 100,$maxHeight = 100,$imgQuality=100)
{
list($width, $height, $type, $attr) = getimagesize($srcImage);
switch ($type) {
case 1: $img = imagecreatefromgif($srcImage); break;
case 2: $img = imagecreatefromjpeg($srcImage); break;
case 3: $img = imagecreatefrompng($srcImage); break;
}
$scale = min($maxWidth/$width, $maxHeight/$height); //求出绽放比例
if($scale < 1) {
$newWidth = floor($scale*$width);
$newHeight = floor($scale*$height);
$newImg = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($newImg, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
$newName = "";
switch($type) {
case 1: if(imagegif($newImg, "$toFile$newName.gif", $imgQuality))
return "$newName.gif"; break;
case 2: if(imagejpeg($newImg, "$toFile$newName.jpg", $imgQuality))
return "$newName.jpg"; break;
case 3: if(imagepng($newImg, "$toFile$newName.png", $imgQuality))
return "$newName.png"; break;
default: if(imagejpeg($newImg, "$toFile$newName.jpg", $imgQuality))
return "$newName.jpg"; break;
}
imagedestroy($newImg);
}
imagedestroy($img);
return false;
}
//直接压制指定大小,不进行等比处理
function resize_2($srcImage,$toFile,$maxWidth = 100,$maxHeight = 100,$imgQuality=100)
{
list($width, $height, $type, $attr) = getimagesize($srcImage);
switch ($type) {
case 1: $img = imagecreatefromgif($srcImage); break;
case 2: $img = imagecreatefromjpeg($srcImage); break;
case 3: $img = imagecreatefrompng($srcImage); break;
}
$scale = min($maxWidth/$width, $maxHeight/$height); //求出绽放比例
$newWidth = $maxWidth;
$newHeight = $maxHeight;
$newImg = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($newImg, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
$newName = "";
switch($type) {
case 1: if(imagegif($newImg, "$toFile$newName.gif", $imgQuality))
return "$newName.gif"; break;
case 2: if(imagejpeg($newImg, "$toFile$newName.jpg", $imgQuality))
return "$newName.jpg"; break;
case 3: if(imagepng($newImg, "$toFile$newName.png", $imgQuality))
return "$newName.png"; break;
default: if(imagejpeg($newImg, "$toFile$newName.jpg", $imgQuality))
return "$newName.jpg"; break;
imagedestroy($newImg);
}
imagedestroy($img);
return false;
}//裁剪图片
function cutImg($o_file,$x1,$x2,$y1,$y2){
//设置文件参数
//$o_file="./uploadfiles/200902/13/1234515154_QcA.jpg";//原文件文件路径
$file=basename($o_file);//文件
$ext=end(explode(".", $file));//扩展名
$filename=basename($file,$ext);//文件名 $filelen=strlen($file);
$path=substr($o_file,0,strlen(($o_file))-$filelen);//文件夹 $newfile=$path."edit_".$file;//新文件名
$newthufile=$path."thumb_".$file;//新文件名 header('Content-type: image/jpeg');
list($width, $height) = getimagesize($o_file); $new_width = $x2-$x1;
$new_height = $y2-$y1;
$image_n = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($o_file); imagecopyresampled($image_n, $image, 0, 0, $x1, $y1, $new_width, $new_height, $new_width, $new_height);
//输出文件
imagejpeg($image_n, $newfile, 100);
$newfileName = $newfile;
return $newfile;
}利用上面的函数将指定大小的图片按指定路径生成出来。再进行调用。
queryphp框架自带无损缩放,无压扁剪切(就是缩放到指定大小,而图像不会被压扁或拉伸了),可以定动剪切头像。http://code.google.com/p/queryphp/downloads/list