一个图片生成多个尺寸的图片

Posted 进击的巨人X

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一个图片生成多个尺寸的图片相关的知识,希望对你有一定的参考价值。

/**
* 图片显示处理
* *
*/

public function handleProductsImg(){

//视频列表
$model_video = new VideoModel();
$where = array();
$where[] = array(‘video_name‘,‘neq‘,‘‘);
$where[] = array(‘video_id‘,‘gt‘,79);
$data1 = $model_video->getVideoAll($where);

$pro_ids = array_column($data1, ‘products_commonid‘);
// dump($pro_ids);

$model_products = new ProductsModel();

$condition = array();
$condition[] = array(‘products_commonid‘,‘in‘,$pro_ids);
$condition[] = array(‘products_id‘,‘neq‘,108868);
// $_SESSION[‘limit1‘] = 0;
dump($_SESSION);
// dump($_SESSION[‘limit1‘]);
if (!$_SESSION[‘limit1‘]) {
$_SESSION[‘limit1‘] = 0;

}
// dump($_SESSION);die;
// $_SESSION[‘limit1‘] = 0;
$limit = ($_SESSION[‘limit1‘] * 100).","."100";
// $condition[] = array(‘products_state|products_verify‘,‘neq‘,1);
// $condition[] = array(‘products_verify‘,‘=‘,1);
$products_info = $model_products->getProductsListNoPage($condition,‘products_id,products_image,store_id‘,‘‘,‘‘,$limit);

// $products_ids = array_column($products_info, ‘products_id‘);
if (!$products_info) {
dump(123333);die;
}
// dump($products_info);die;


// 判断图片数量是否超限
// $model_album = new AlbumModel();
/*$condition = array();
$condition[] = array(‘store_id‘,‘=‘,$store_id);
$condition[] = array(‘is_default‘,‘=‘,1);
$class_info = $model_album->getOne($condition, ‘album_class‘);*/
//按上传年月日存放,例如2011/04/19/a.jpg
$subpath = date("Y",time()) . "/" . date("m",time()) . "/" . date("d",time()) . "/";
foreach ($products_info as $key => $value) {
//店铺图片分享,增加水印图片
if (@fopen(cthumb($value[‘products_image‘],360,$value[‘store_id‘]),‘r‘)) {
//默认图片
continue;
// return UPLOAD_SITE_URL.DS.‘watermark‘.DS.‘weixin_share.jpg‘;
}

// $this->resize_image(cthumb($value[‘products_image‘]),cthumb($value[‘products_image‘],360,$value[‘store_id‘]),360,360);
$fileName60 = $this->resize_image(cthumb($value[‘products_image‘]),$value[‘products_image‘],$value[‘store_id‘],‘_60‘,60,60);
$fileName240 = $this->resize_image(cthumb($value[‘products_image‘]),$value[‘products_image‘],$value[‘store_id‘],‘_240‘,240,240);
$fileName360 = $this->resize_image(cthumb($value[‘products_image‘]),$value[‘products_image‘],$value[‘store_id‘],‘_360‘,360,360);
$fileName1280 = $this->resize_image(cthumb($value[‘products_image‘]),$value[‘products_image‘],$value[‘store_id‘],‘_1280‘,1280,1280);


dump($fileName60);
dump($fileName240);
dump($fileName360);
dump($fileName1280);

$data[$key][‘products_id‘] = $_SESSION[‘products_id‘][] = $value[‘products_id‘];
$data[$key][‘products_image‘] = $_SESSION[‘products_image‘][] = $fileName360;
}

$_SESSION[‘limit1‘] = $_SESSION[‘limit1‘]+1;
dump($_SESSION);
dump($data);die;
}
/**
* 按照指定的尺寸压缩图片
* @param $source_path 原图路径
* @param $target_path 保存路径
* @param $imgWidth 目标宽度
* @param $imgHeight 目标高度
* @param $store_id 店铺id
* @param $re 前缀
* @return bool|string
*/
private function resize_image($source_path,$target_path,$store_id,$pre,$imgWidth,$imgHeight)
{
$source_info = getimagesize($source_path);
$source_mime = $source_info[‘mime‘];
switch ($source_mime)
{
case ‘image/gif‘:
$source_image = imagecreatefromgif($source_path);
break;
case ‘image/jpeg‘:
$source_image = imagecreatefromjpeg($source_path);
break;
case ‘image/png‘:
$source_image = imagecreatefrompng($source_path);
break;
default:
return false;
break;
}
$target_image = imagecreatetruecolor($imgWidth, $imgHeight); //创建一个彩色的底图
imagecopyresampled($target_image, $source_image, 0, 0, 0, 0, $imgWidth, $imgHeight, $source_info[0], $source_info[1]);


//获取文件拓展名
$ext = $name = strstr($target_path,‘.‘);//strtolower(pathinfo($target_path, PATHINFO_EXTENSION));
$name = strstr($target_path,‘.‘,true);
$header = strstr($source_path,$target_path,true);

//按上传年月日存放,例如2011/04/19/a.jpg
$subpath = date("Y",time()) . "/" . date("m",time()) . "/" . date("d",time()) . "/";
// $apic_cover = $subpath .$apic_cover_pic;
$img_path = $name . $pre . $ext;
$fileName = BASE_UPLOAD_PATH . DS . ATTACH_PRODUCTS . DS . $store_id . DS . $img_path;//$header.$name.‘_360‘.$ext;

dump(‘这里是原路径‘.$source_path);
// dump($ext);
// dump($fileName);
// dump(‘./‘.$fileName);
// if(!imagejpeg($target_image,$fileName)){
if(!imagejpeg($target_image,$fileName)){
$fileName = ‘‘;
}
imagedestroy($target_image);
// dump($fileName);
return $fileName;
}

以上是关于一个图片生成多个尺寸的图片的主要内容,如果未能解决你的问题,请参考以下文章

Python批量生成特定尺寸图片及图画任意文字

基于.NetCore开发博客项目 StarBlog - (15) 生成随机尺寸图片

python库pillow:实现生成图片并加水印

怎么把图片中的二维码提出来?

python处理图片数据?

高并发图片(缩略图)处理中间层服务架构设计