图片处理类imageclsphp
<?php/**图片处理类 */class imagecls{/** * 文件信息 */var $file = array();/** * 保存目录 */var $dir = ;/** * 错误代码 */var $error_code = ;/** * 文件上传最大KB */var $max_size = ;function es_imagecls(){} private function checkSize($size) { return !($size > $this>max_size) || ( == $this>max_size); } /** * 处理上传文件 * @param array $file 上传的文件 * @param string $dir 保存的目录 * @return bool */function init($file $dir = temp){if(!is_array($file) || empty($file) || !$this>isUploadFile($file[tmp_name]) || trim($file[name]) == || $file[size] == ){$this>file = array();$this>error_code = ;return false;}else{$file[size] = intval($file[size]);$file[name] = trim($file[name]);$file[thumb] = ;$file[ext] = $this>fileExt($file[name]);$file[name] = htmlspecialchars($file[name] ENT_QUOTES);$file[is_image] = $this>isImageExt($file[ext]);$file[file_dir] = $this>getTargetDir($dir);$file[prefix] = md(microtime(true))rand();$file[target] = "/public/"$file[file_dir]/$file[prefix]jpg; //相对$file[local_target] = APP_ROOT_PATH"public/"$file[file_dir]/$file[prefix]jpg;//物理$this>file = &$file;$this>error_code = ;return true;}}/** * 保存文件 * @return bool */function save(){if(empty($this>file) || empty($this>file[tmp_name]))$this>error_code = ;elseif(!$this>checkSize($this>file[size]))$this>error_code = ;elseif(!$this>file[is_image])$this>error_code = ;elseif(!$this>saveFile($this>file[tmp_name] $this>file[local_target]))$this>error_code = ; elseif($this>file[is_image] && (!$this>file[image_info] = $this>getImageInfo($this>file[local_target] true))){$this>error_code = ;@unlink($this>file[local_target]);}else{$this>error_code = ;return true;}return false;}/** * 获取错误代码 * @return number */function error(){return $this>error_code;}/** * 获取文件扩展名 * @return string */function fileExt($file_name){return addslashes(strtolower(substr(strrchr($file_name ) )));}/** * 根据扩展名判断文件是否为图像 * @param string $ext 扩展名 * @return bool */function isImageExt($ext){static $img_ext = array(jpg jpeg png bmpgifgiff);return in_array($ext $img_ext) ? : ;}/** * 获取图像信息 * @param string $target 文件路径 * @return mixed */function getImageInfo($target){$ext = es_imagecls::fileExt($target);$is_image = es_imagecls::isImageExt($ext);if(!$is_image)return false;elseif(!is_readable($target))return false;elseif($image_info = @getimagesize($target)){list($width $height $type) = !empty($image_info) ? $image_info : array( );$size = $width * $height;if($is_image && !in_array($type array()))return false;$image_info[type] = strtolower (substr(image_type_to_extension($image_info[])));return $image_info;}elsereturn false;}/** * 获取是否充许上传文件 * @param string $source 文件路径 * @return bool */function isUploadFile($source){return $source && ($source != none) && (is_uploaded_file($source) || is_uploaded_file(str_replace( $source)));}/** * 获取保存的路径 * @param string $dir 指定的保存目录 * @return string */function getTargetDir($dir){ if (!is_dir(APP_ROOT_PATH"public/"$dir)) { @mkdir(APP_ROOT_PATH"public/"$dir); @chmod(APP_ROOT_PATH"public/"$dir ); } return $dir;}/** * 保存文件 * @param string $source 源文件路径 * @param string $target 目录文件路径 * @return bool */private function saveFile($source $target){if(!es_imagecls::isUploadFile($source))$succeed = false;elseif(@copy($source $target))$succeed = true;elseif(function_exists(move_uploaded_file) && @move_uploaded_file($source $target))$succeed = true;elseif (@is_readable($source) && (@$fp_s = fopen($source rb)) && (@$fp_t = fopen($target wb))){while (!feof($fp_s)){$s = @fread($fp_s * );@fwrite($fp_t $s);}fclose($fp_s);fclose($fp_t);$succeed = true;}if($succeed){$this>error_code = ;@chmod($target );@unlink($source);}else{$this>error_code = ;}return $succeed;}public function thumb($image$maxWidth=$maxHeight=$gen = $interlace=true$filepath = $is_preview = true) { $info = es_imagecls::getImageInfo($image); if($info !== false){ $srcWidth = $info[]; $srcHeight = $info[];$type = $info[type]; $interlace = $interlace? :; unset($info);if($maxWidth > && $maxHeight > )$scale = min($maxWidth/$srcWidth $maxHeight/$srcHeight); // 计算缩放比例elseif($maxWidth == )$scale = $maxHeight/$srcHeight;elseif($maxHeight == )$scale = $maxWidth/$srcWidth;$paths = pathinfo($image);$paths[filename] = trim(strtolower($paths[basename]) ""strtolower($paths[extension]));$basefilename = explode("_"$paths[filename]);$basefilename = $basefilename[];if(empty($filepath)){if($is_preview)$thumbname = $paths[dirname]/$basefilename _$maxWidthx$maxHeightjpg;else$thumbname = $paths[dirname]/$basefilename o_$maxWidthx$maxHeightjpg;}else$thumbname = $filepath;$thumburl = str_replace(APP_ROOT_PATH/$thumbname); if($scale >= ){ // 超过原图大小不再缩略 $width = $srcWidth; $height = $srcHeight; if(!$is_preview) { //非预览模式写入原图 file_put_contents($thumbnamefile_get_contents($image)); //用原图写入 return array(url=>$thumburlpath=>$thumbname); } }else{ // 缩略图尺寸 $width = (int)($srcWidth*$scale); $height = (int)($srcHeight*$scale); }if($gen == ){$width = $maxWidth;$height = $maxHeight;} // 载入原图 $createFun = imagecreatefrom($type==jpg?jpeg:$type);if(!function_exists($createFun))$createFun = imagecreatefromjpeg; $srcImg = $createFun($image); //创建缩略图 if($type!=gif && function_exists(imagecreatetruecolor)) $thumbImg = imagecreatetruecolor($width $height); else $thumbImg = imagecreate($width $height);$x = ;$y = ;if($gen == && $maxWidth > && $maxHeight > ){$resize_ratio = $maxWidth/$maxHeight;$src_ratio = $srcWidth/$srcHeight;if($src_ratio >= $resize_ratio){$x = ($srcWidth ($resize_ratio * $srcHeight)) / ;$width = ($height * $srcWidth) / $srcHeight;}else{$y = ($srcHeight ( ( / $resize_ratio) * $srcWidth)) / ;$height = ($width * $srcHeight) / $srcWidth;}} // 复制图片 if(function_exists("imagecopyresampled")) imagecopyresampled($thumbImg $srcImg $x $y $width $height $srcWidth$srcHeight); else imagecopyresized($thumbImg $srcImg $x $y $width $height $srcWidth$srcHeight); if(gif==$type || png==$type) { $background_color = imagecolorallocate($thumbImg ); // 指派一个绿色imagecolortransparent($thumbImg$background_color); // 设置为透明色若注释掉该行则输出绿色的图 } // 对jpeg图形设置隔行扫描 if(jpg==$type || jpeg==$type)imageinterlace($thumbImg$interlace); // 生成图片 imagejpeg($thumbImg$thumbname); imagedestroy($thumbImg); imagedestroy($srcImg);return array(url=>$thumburlpath=>$thumbname); } return false; }public function make_thumb($srcImg$srcWidth$srcHeight$type$maxWidth= $maxHeight=$gen = ) { $interlace = $interlace? :;if($maxWidth > && $maxHeight > )$scale = min($maxWidth/$srcWidth $maxHeight/$srcHeight); // 计算缩放比例elseif($maxWidth == )$scale = $maxHeight/$srcHeight;elseif($maxHeight == )$scale = $maxWidth/$srcWidth; if($scale >= ){ // 超过原图大小不再缩略 $width = $srcWidth; $height = $srcHeight; }else{ // 缩略图尺寸 $width = (int)($srcWidth*$scale); $height = (int)($srcHeight*$scale); }if($gen == ){$width = $maxWidth;$height = $maxHeight;} //创建缩略图 if($type!=gif && function_exists(imagecreatetruecolor)) $thumbImg = imagecreatetruecolor($width $height); else $thumbImg = imagecreatetruecolor($width $height);$x = ;$y = ;if($gen == && $maxWidth > && $maxHeight > ){$resize_ratio = $maxWidth/$maxHeight;$src_ratio = $srcWidth/$srcHeight;if($src_ratio >= $resize_ratio){$x = ($srcWidth ($resize_ratio * $srcHeight)) / ;$width = ($height * $srcWidth) / $srcHeight;}else{$y = ($srcHeight ( ( / $resize_ratio) * $srcWidth)) / ;$height = ($width * $srcHeight) / $srcWidth;}} // 复制图片 if(function_exists("imagecopyresampled")) imagecopyresampled($thumbImg $srcImg $x $y $width $height $srcWidth$srcHeight); else imagecopyresized($thumbImg $srcImg $x $y $width $height $srcWidth$srcHeight); if(gif==$type || png==$type) { $background_color = imagecolorallocate($thumbImg ); // 指派一个绿色imagecolortransparent($thumbImg$background_color); // 设置为透明色若注释掉该行则输出绿色的图 } // 对jpeg图形设置隔行扫描 if(jpg==$type || jpeg==$type)imageinterlace($thumbImg$interlace); return $thumbImg; } public function water($source$water$alpha=$position="") { //检查文件是否存在 if(!file_exists($source)||!file_exists($water)) return false; //图片信息 $sInfo = es_imagecls::getImageInfo($source); $wInfo = es_imagecls::getImageInfo($water); //如果图片小于水印图片不生成图片 if($sInfo[""] < $wInfo[""] || $sInfo[] < $wInfo[]) return false; if(is_animated_gif($source)) { require_once APP_ROOT_PATH"system/utils/gif_encoderphp";require_once APP_ROOT_PATH"system/utils/gif_readerphp";$gif = new GIFReader();$gif>load($source);foreach($gif>IMGS[frames] as $k=>$img){$im = imagecreatefromstring($gif>getgif($k));//为im加水印$sImage=$im; $wCreateFun="imagecreatefrom"$wInfo[type];if(!function_exists($wCreateFun))$wCreateFun = imagecreatefromjpeg; $wImage=$wCreateFun($water); //设定图像的混色模式 imagealphablending($wImage true); switch (intval($position)) { case : break; //左上 case : $posY=; $posX=; //生成混合图像imagecopymerge($sImage $wImage $posX $posY $wInfo[]$wInfo[]$alpha); break; //右上 case : $posY=; $posX=$sInfo[]$wInfo[]; //生成混合图像imagecopymerge($sImage $wImage $posX $posY $wInfo[]$wInfo[]$alpha); break; //左下 case : $posY=$sInfo[]$wInfo[]; $posX=; //生成混合图像imagecopymerge($sImage $wImage $posX $posY $wInfo[]$wInfo[]$alpha); break; //右下 case : $posY=$sInfo[]$wInfo[]; $posX=$sInfo[]$wInfo[]; //生成混合图像imagecopymerge($sImage $wImage $posX $posY $wInfo[]$wInfo[]$alpha); break; //居中 case : $posY=$sInfo[]/$wInfo[]/; $posX=$sInfo[]/$wInfo[]/; //生成混合图像imagecopymerge($sImage $wImage $posX $posY $wInfo[]$wInfo[]$alpha); break; }//end im加水印ob_start();imagegif($sImage);$content = ob_get_contents(); ob_end_clean();$frames [ ] = $content; $framed [ ] = $img[frameDelay];}$gif_maker = new GIFEncoder ( $frames $framed "bin" //bin为二进制 url为地址 );$image_rs = $gif_maker>GetAnimation ( );//如果没有给出保存文件名默认为原图像名 @unlink($source); //保存图像 file_put_contents($source$image_rs); return true; } //建立图像$sCreateFun="imagecreatefrom"$sInfo[type];if(!function_exists($sCreateFun))$sCreateFun = imagecreatefromjpeg;$sImage=$sCreateFun($source); $wCreateFun="imagecreatefrom"$wInfo[type];if(!function_exists($wCreateFun))$wCreateFun = imagecreatefromjpeg; $wImage=$wCreateFun($water); //设定图像的混色模式 imagealphablending($wImage true); switch (intval($position)) { case : break; //左上 case : $posY=; $posX=; //生成混合图像imagecopymerge($sImage $wImage $posX $posY $wInfo[]$wInfo[]$alpha); break; //右上 case : $posY=; $posX=$sInfo[]$wInfo[]; //生成混合图像imagecopymerge($sImage $wImage $posX $posY $wInfo[]$wInfo[]$alpha); break; //左下 case : $posY=$sInfo[]$wInfo[]; $posX=; //生成混合图像imagecopymerge($sImage $wImage $posX $posY $wInfo[]$wInfo[]$alpha); break; //右下 case : $posY=$sInfo[]$wInfo[]; $posX=$sInfo[]$wInfo[]; //生成混合图像imagecopymerge($sImage $wImage $posX $posY $wInfo[]$wInfo[]$alpha); break; //居中 case : $posY=$sInfo[]/$wInfo[]/; $posX=$sInfo[]/$wInfo[]/; //生成混合图像 imagecopymerge($sImage $wImage $posX $posY $wInfo[]$wInfo[]$alpha); break; } //如果没有给出保存文件名默认为原图像名 @unlink($source); //保存图像 imagejpeg($sImage$source); imagedestroy($sImage); }}if(!function_exists(image_type_to_extension)){function image_type_to_extension($imagetype){if(empty($imagetype))return false;switch($imagetype){case IMAGETYPE_GIF : return gif;case IMAGETYPE_JPEG : return jpeg;case IMAGETYPE_PNG : return png;case IMAGETYPE_SWF : return swf;case IMAGETYPE_PSD : return psd;case IMAGETYPE_BMP : return bmp;case IMAGETYPE_TIFF_II : return tiff;case IMAGETYPE_TIFF_MM : return tiff;case IMAGETYPE_JPC : return jpc;case IMAGETYPE_JP : return jp;case IMAGETYPE_JPX : return jpf;case IMAGETYPE_JB : return jb;case IMAGETYPE_SWC : return swc;case IMAGETYPE_IFF : return aiff;case IMAGETYPE_WBMP : return wbmp;case IMAGETYPE_XBM : return xbm;default : return false;}}}?>
get_spec_img()调用图片类然后再用下面的方法保存不同规格的图片并返回图片连接
//获取相应规格的图片地址//gen=:保持比例缩放不剪裁如高为则保证宽度按比例缩放 gen=保证长宽剪裁function get_spec_image($img_path$width=$height=$gen=$is_preview=true){if($width==)$new_path = $img_path;else{$img_name = substr($img_path);$img_ext = substr($img_path);if($is_preview)$new_path = $img_name"_"$width"x"$height"jpg";else$new_path = $img_name"o_"$width"x"$height"jpg";if(!file_exists($new_path)){require_once "imageclsphp";$imagec = new imagecls();$thumb = $imagec>thumb($img_path$width$height$gentrue" "$is_preview);if(app_conf("PUBLIC_DOMAIN_ROOT")!=) { $paths = pathinfo($new_path); $path = str_replace("/"""$paths[dirname]); $filename = $paths[basename]; $pathwithoupublic = str_replace("public/"""$path); $file_data = @file_get_contents($path$file); $img = @imagecreatefromstring($file_data); if($img!==false) { $save_path = "public/"$path; if(!is_dir($save_path)) { @mk_dir($save_path); } @file_put_contents($save_path$name$file_data); } }}}return $new_path;}
使用方法:
//im:将店铺图片保存为种规格:小图:x中图x大图x$small_url=get_spec_image($data[image]);$<span id="result_box" class="short_text" lang="en"> <span>middle_url</span></span>=get_spec_image($data[image]);$big_url=get_spec_image($data[image]);