找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 230|回复: 0
打印 上一主题 下一主题

基于PHP服务端图片生成缩略图的方法详解

[复制链接]

2617

主题

2617

帖子

7789

积分

论坛元老

Rank: 8Rank: 8

积分
7789
跳转到指定楼层
楼主
发表于 2018-2-14 08:17:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

            [U]复制代码[/U] 代码如下:
1,
              '200_100'=> 1
           );
$imagePath = "../image/";
function parseUrl($url){
   preg_match("/(?P[\w\d]+)_w(?P\d+)_h(?P\d+)\.(?P\w+)/",$url,$match);
   return $match;
}
$urlArr = explode("/",$_SERVER['REQUEST_URI']);
$$urlArr[count($urlArr)-1];
$picInfo = parseUrl($);
//错误尺寸
if(empty($picInfo['width']) || empty($picInfo['height']) ||
!array_key_exists($picInfo['width'].'_'.$picInfo['height'],$picSize)) die('不存在该尺寸图片');
$originalPic = $imagePath.$picInfo['name'].'/'.$picInfo['name'].'.'.$picInfo['ext'];
//原始图不存在
if(!file_exists($originalPic)) die("图片不存在!");
/**
*等比例压缩图片
*/
switch($picInfo['ext']){
   case 'jpg':
      $orgImg = ImageCreateFromJpeg($originalPic);
      break;
   default:
      break;
}
$owidth  =  ImageSX($orgImg); //原始尺寸
$oheight =  ImageSY($orgImg);
$tW = $picInfo['width'];
$tH = $picInfo['height'];
//获取缩略图尺寸
if($owidth/$oheight > $tW/$tH){
    $tH = intval($tW * $oheight/$owidth);
}else{
     $tW = intval($tH * $owidth/$oheight);
}
//生成背景图
$new_($picInfo['width'], $picInfo['height']);
$bgColor = imagecolorallocate($new_img,255,255,255);
if (!@imagefilledrectangle($new_img, 0, 0, $picInfo['width']-1, $picInfo['height']-1, $bgColor)) {
    echo "无法创建背景图";  //@todo记录日志
    exit(0);
}
if (!@imagecopyresampled($new_img, $orgImg, ($picInfo['width']-$tW)/2, ($picInfo['height']-$tH)/2, 0, 0, $tW, $tH, $owidth, $oheight)) {
    echo "生成图片失败";
    exit(0);
}
//生成图片
ob_start();
imagejpeg($new_img);
$_newImg = ob_get_contents();
ob_end_clean();
file_put_contents($imagePath.$picInfo['name']."/".$, $_newImg);
header("Content-type:image/jpeg; charset=utf-8");
imagejpeg($new_img);
?>
使用时候绑定apache conf 的 documentError 404 的handler 为此文件。。
            
            
您可能感兴趣的文章:
  • PHP用GD库生成高质量的缩略图片
  • php图片的裁剪与缩放生成符合需求的缩略图
  • 使用gd库实现php服务端图片裁剪和生成缩略图功能分享
  • php实现上传图片生成缩略图示例
  • jquery+php+ajax显示上传进度的多图片上传并生成缩略图代码
  • php实现按指定大小等比缩放生成上传图片缩略图的方法
  • 基于GD2图形库的PHP生成图片缩略图类代码分享
  • PHP批量生成图片缩略图的方法
  • 使用PHP生成图片的缩略图的方法
  • php上传图片生成缩略图(GD库)
  • PHP Imagick完美实现图片裁切、生成缩略图、添加水印
  • php实现图片上传并利用ImageMagick生成缩略图
  • php生成图片缩略图功能示例
            
  • 分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    用户反馈
    客户端