求 php 图片压缩代码 学习中,php可以生成缩略图吗, 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 可以生成function createThumbImage($original,$thumb_path,$new_width=50,$new_height=50,$ext=null){ if ($ext==null) { $ext=end(explode(".",$original)); } $ext=strtolower($ext); $filename = $original; list($width, $height) = getimagesize($filename); $image_p = imagecreatetruecolor($new_width, $new_height); switch ($ext) { case 'jpg': $image = imagecreatefromjpeg($filename); imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); imagejpeg($image_p, $thumb_path, 100); return true; case 'gif': $image = imagecreatefromgif($filename); imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); imagegif($image_p, $thumb_path, 100); return true; case 'png': $image = imagecreatefrompng($filename); imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); imagepng($image_p, $thumb_path, 100); return true; } return false;}这段代码我参考ice_berg的代码的,忘记了是否经过修改。。 摘自:木目子技术交流站/** * 生成一个缩略图 * @params $Image:String; * @return String; */ function make2small($Image) { $info=$this->getInfo($Image); if(!$info) { return 0; } $file=$this->DOC_ROOT.$this->Path."/".$Image; /*随即生成一个文件名*/ $small=$this->make_rand(16).$this->mimePhoto[$info["mime"]]; if(@file_exists($this->DOC_ROOT.$this->Path."/".$small)) { return $this->make2small($Image); } /*如果大图小于小缩略图,则直接拷贝一份*/ if($this->width>=$info["width"] && $this->heigh>=$info["heigh"]) { if(@copy($file,$this->DOC_ROOT.$this->smallPath."/".$small)) { return $small; }else{ return 0; } } /*按照比例得到缩略图的高、宽*/ if(($info["width"] / $info["heigh"]) > ($this->width / $this->heigh)) { $w=$this->width; $h=floor($info["heigh"]*$w / $info["width"]); }else{ $h=$this->heigh; $w=floor($info["width"]*$h / $info["heigh"]); } /*创建缩略图*/ switch($info["mime"]) { case "image/gif": $im=@imagecreatefromgif($file); break; case "image/pjpeg": case "image/jpeg": $im=@imagecreatefromjpeg($file); break; case "image/png": $im=@imagecreatefrompng($file); break; default: return -1; } if($im) { $ni=@imagecreatetruecolor($w,$h); @imagecopyresampled($ni,$im,0,0,0,0,$w,$h,$info["width"],$info["heigh"]); $result=@imagejpeg($ni,$this->DOC_ROOT.$this->smallPath."/".$small); @imagedestroy($ni); } @imagedestroy($im); if($result) { return $small; } return "NoSmall.gif"; } PHP 制作了一个计算器,出了点问题,求解! 请各路大虾帮忙解决问题 函数返回两个以上结果的方法 输入日期时如何实现点击按钮出现日历,点日期自已填入输入控件中? 怎么才能统计用户在登陆时输入10次错误 一个正则问题 图片显示问题!急!! 为什么程序文件就不能requice("/src/db.php"),而资源可以用/photo/1.gif方式防问, 讨论:关于PHP实现多服务器WEB同步问题? 关于MYSQL的连接。 想把字符串(二维数组)写入mysql 紧急求助-服务器不支持cookie
{
if ($ext==null)
{
$ext=end(explode(".",$original));
}
$ext=strtolower($ext);
$filename = $original;
list($width, $height) = getimagesize($filename);
$image_p = imagecreatetruecolor($new_width, $new_height); switch ($ext)
{
case 'jpg':
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($image_p, $thumb_path, 100);
return true;
case 'gif':
$image = imagecreatefromgif($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagegif($image_p, $thumb_path, 100);
return true;
case 'png':
$image = imagecreatefrompng($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagepng($image_p, $thumb_path, 100);
return true;
}
return false;
}
这段代码我参考ice_berg的代码的,忘记了是否经过修改。。
/**
* 生成一个缩略图
* @params $Image:String;
* @return String;
*/
function make2small($Image)
{
$info=$this->getInfo($Image);
if(!$info)
{
return 0;
} $file=$this->DOC_ROOT.$this->Path."/".$Image; /*随即生成一个文件名*/
$small=$this->make_rand(16).$this->mimePhoto[$info["mime"]]; if(@file_exists($this->DOC_ROOT.$this->Path."/".$small))
{
return $this->make2small($Image);
} /*如果大图小于小缩略图,则直接拷贝一份*/
if($this->width>=$info["width"] && $this->heigh>=$info["heigh"])
{
if(@copy($file,$this->DOC_ROOT.$this->smallPath."/".$small))
{
return $small;
}else{
return 0;
}
}
/*按照比例得到缩略图的高、宽*/
if(($info["width"] / $info["heigh"]) > ($this->width / $this->heigh))
{
$w=$this->width;
$h=floor($info["heigh"]*$w / $info["width"]);
}else{
$h=$this->heigh;
$w=floor($info["width"]*$h / $info["heigh"]);
}
/*创建缩略图*/
switch($info["mime"])
{
case "image/gif":
$im=@imagecreatefromgif($file);
break;
case "image/pjpeg":
case "image/jpeg":
$im=@imagecreatefromjpeg($file);
break;
case "image/png":
$im=@imagecreatefrompng($file);
break;
default:
return -1;
}
if($im)
{
$ni=@imagecreatetruecolor($w,$h);
@imagecopyresampled($ni,$im,0,0,0,0,$w,$h,$info["width"],$info["heigh"]);
$result=@imagejpeg($ni,$this->DOC_ROOT.$this->smallPath."/".$small);
@imagedestroy($ni);
} @imagedestroy($im);
if($result)
{
return $small;
}
return "NoSmall.gif";
}