图片压缩处理
作者QQ:67065435 QQ群:669756510
本站内容全部为作者原创,转载请注明出处!
注意
# 要启用gd扩展库并启用以下支持 jpg,png,gif,wbmp,webp图片缩放为360x480
<?php //缩放后的宽高 $width = 360; $height = 480; //获取背景图指针 $dst_resource = imagecreatetruecolor($width, $height); $dst_color = imagecolorallocatealpha($dst_resource, 0, 0, 0, 127); imagesavealpha($dst_resource, true); imagefill($dst_resource, 0, 0, $dst_color); //要缩放的图片 $img_path = __DIR__ . '/image.jpg'; //获取要缩放图片属性 $img_size = getimagesize($img_path); //根据要缩放图片mime类型获取要缩放图片指针 if (strstr($img_size['mime'], 'jpg') || strstr($img_size['mime'], 'jpeg')) { $img_resource = imagecreatefromjpeg($img_path); } elseif (strstr($img_size['mime'], 'png')){ $img_resource = imagecreatefrompng($img_path); } elseif (strstr($img_size['mime'], 'gif')){ $img_resource = imagecreatefromgif($img_path); } elseif (strstr($img_size['mime'], 'wbmp')){ $img_resource = imagecreatefromwbmp($img_path); } else { $img_resource = imagecreatefromjpeg($img_path); } //把要缩放图片缩放到背景图上覆盖 imagecopyresampled($dst_resource, $img_resource, 0, 0, 0, 0, $width, $height, $img_size[0], $img_size[1]); if(要返回图片buffer){ ob_start(); imagejpeg($dst_resource, null, 100); $img_buffer = ob_get_contents(); ob_end_clean(); imagedestroy($img_resource); imagedestroy($dst_resource); return $img_buffer; } elseif (要保存图片) { $save_path = __DIR__ . '/image.jpg'; imagejpeg($dst_resource, $save_path, 100); imagedestroy($img_resource); imagedestroy($dst_resource); return true; }图片缩放为360x480添加文字水印
<?php //缩放后的宽高 $width = 360; $height = 480; //获取背景图指针 $dst_resource = imagecreatetruecolor($width, $height); $dst_color = imagecolorallocatealpha($dst_resource, 0, 0, 0, 127); imagesavealpha($dst_resource, true); imagefill($dst_resource, 0, 0, $dst_color); //要缩放的图片 $img_path = __DIR__ . '/image.jpg'; //获取要缩放图片属性 $img_size = getimagesize($img_path); //根据要缩放图片mime类型获取要缩放图片指针 if (strstr($img_size['mime'], 'jpg') || strstr($img_size['mime'], 'jpeg')) { $img_resource = imagecreatefromjpeg($img_path); } elseif (strstr($img_size['mime'], 'png')){ $img_resource = imagecreatefrompng($img_path); } elseif (strstr($img_size['mime'], 'gif')){ $img_resource = imagecreatefromgif($img_path); } elseif (strstr($img_size['mime'], 'wbmp')){ $img_resource = imagecreatefromwbmp($img_path); } else { $img_resource = imagecreatefromjpeg($img_path); } //把要缩放图片缩放到背景图上覆盖 imagecopyresampled($dst_resource, $img_resource, 0, 0, 0, 0, $width, $height, $img_size[0], $img_size[1]); //获取文字水印指针 $r = 0;//红0-255 $g = 0;//绿0-255 $b = 0;//蓝0-255 $a = 0;//透0-127(越大越透明) $angle = 0;//角度 $font_x = 0;//横坐标 $font_y = 16;//纵坐标 $font_size = 16;//字号 $content = '文字';//内容 //文字水印宽度 $content_width = mb_strlen($content) * $font_size; //文字水印高度 $content_height = $font_size; $font_path = __DIR__ . '/ncover.ttf';//中文字体文件 //获取文字水印指针 $fgr_resource = imagecreatetruecolor($content_width,$content_height); $dst_color = imagecolorallocatealpha($dst_resource, 0, 0, 0, 127); imagesavealpha($dst_resource, true); imagefill($dst_resource, 0, 0, $dst_color); //设置水印文字属性(颜色、透明度) $color = imagecolorallocatealpha($fgr_resource, $r, $g, $b, $a); //将文字水印放到背景图当中 imagettftext($dst_resource, $font_size, $angle, $font_x, $font_y, $color, $font_path, $content); if(要返回图片buffer){ ob_start(); imagejpeg($dst_resource, null, 100); $img_buffer = ob_get_contents(); ob_end_clean(); imagedestroy($img_resource); imagedestroy($dst_resource); return $img_buffer; } elseif (要保存图片) { $save_path = __DIR__ . '/image.jpg'; imagejpeg($dst_resource, $save_path, 100); $img_buffer = ob_get_contents(); imagedestroy($img_resource); imagedestroy($dst_resource); return true; }base64缩放为360x480
<?php $img_base64 = 'data:image/png;base64,/************'; //缩放后的宽高 $width = 360; $height = 480; //获取背景图指针 $dst_resource = imagecreatetruecolor($width, $height); $dst_color = imagecolorallocatealpha($dst_resource, 0, 0, 0, 127); imagesavealpha($dst_resource, true); imagefill($dst_resource, 0, 0, $dst_color); //获取要缩放图片属性 $img_resource = imagecreatefromstring(base64_decode(preg_replace('/^data:image\/[a-z]+;base64,/', '', $img_base64))); $img_x = imagesx($img_resource); $img_y = imagesy($img_resource); //把要缩放图片缩放到背景图上覆盖 imagecopyresampled($dst_resource, $img_resource, 0, 0, 0, 0, $width, $height, $img_x, $img_y); if(要返回图片buffer){ ob_start(); imagejpeg($dst_resource, null, 100); $img_buffer = ob_get_contents(); ob_end_clean(); imagedestroy($img_resource); imagedestroy($dst_resource); return $img_buffer; } elseif (要保存图片) { $save_path = __DIR__ . '/image.jpg'; imagejpeg($dst_resource, $save_path, 100); imagedestroy($img_resource); imagedestroy($dst_resource); return true; }文字内容转图片
<?php /** * 初始化参数 */ $red = 56; //文字红色(0-255) $green = 56; //文字绿色(0-255) $blue = 56; //文字蓝色(0-255) $alpha = 0; //文字透明(0-127) $angle = 0; //倾斜角度(0-360) $font_size = 20; //文字大小(按像素算) $line_border = 14; //上下行距(按像素算) //解决文字右侧异常问题↓(该参数过小右侧文字溢出,过大右侧有大量留白) $font_border = 6; //文字间距(按像素算) $width = 400; //图片宽度 $lines = 10; //图片行数 $box_border = 10; //左右边距 $quality = 100; //图片质量 $length = floor(($width - $box_border * 2) / ($font_size + $font_border));//每行字数 /** * 初始化资源 */ //获取文本内容 $text_path = __DIR__ . "/text.txt"; //获取背景图片 $back_path = __DIR__ . "/back.jpg"; //获取字体文件 $font_path = __DIR__ . "/font.ttf"; //处理内容文本 $str = trim(file_get_contents($text_path)); $str = preg_replace('/[^\S\r\n]+/', '', $str); $str = " " . preg_replace('/[\r\n]+/', "\n ", $str); $str = explode("\n", $str); $ctt = ""; foreach ($str as $s) { for ($i = 0; $i < mb_strlen($s); $i += $length) { $ctt .= mb_substr($s, $i, $length) . "\n"; } } $ctt = explode("\n", trim($ctt)); /** * 计算主要参数 */ //计算总计行数 $line_count = count($ctt); //计算尾图行数 $line_end = $line_count % $lines; //计算尾图编号 $img_end = (int)ceil($line_count / $lines); /** * 批量生成底片 */ //底片存入文档 $html = ""; //底片指针数组 $dist = []; //背景图片指针 $back = imagecreatefromjpeg($back_path); $back_size = getimagesize($back_path); foreach ($ctt as $ck => $cv) { //计算底片编号 $img_id = (int)ceil(($ck + 1) / $lines); //计算填第几行 $line = ($ck + 1) % $lines; $line = !empty($line) ? $line : $lines; //首行生成底片 if ($line == 1) { //计算底片高度 if ($img_id === $img_end) { $height = ($font_size + $line_border * 2) * $line_end; } else { $height = ($font_size + $line_border * 2) * $lines; } //生成底片资源 $dist[$img_id] = imagecreatetruecolor($width, $height); imagecopyresampled($dist[$img_id], $back, 0, 0, 0, 0, $width, $height, $back_size[0], $back_size[1]); } //本行写入底片 $font_x = $box_border; $font_y = ($font_size + $line_border * 2) * $line - $line_border; $font = imagecreatetruecolor($font_size * $length, $font_size + $line_border); $color = imagecolorallocatealpha($font, $red, $green, $blue, $alpha); imagettftext($dist[$img_id], $font_size, $angle, $font_x, $font_y, $color, $font_path, $cv); imagedestroy($font); //底片存入文档 if ( ($line === $lines) || ($img_id === $img_end && $line === $line_end) ) { ob_start(); imagejpeg($dist[$img_id], null, $quality); imagedestroy($dist[$img_id]); $img_buffer = ob_get_contents(); ob_end_clean(); $img_buffer = 'data:image/jpeg;base64,' . base64_encode($img_buffer); $html .= <<<HTML <img src='{$img_buffer}' style='width:100%;height:auto;'> HTML; } } $html = <<<HTML <div style="margin:0;padding:0;background-color:#fafafa;"> {$html}</div> HTML; imagedestroy($back); echo $html;imagecreatefromjpeg 同功能函数
# 创建指定格式画布 imagecreatefrompng imagecreatefromgif imagecreatefromwbmp imagecreatefromwebpimagecopyresampled 参数说明
# 左上角为坐标原点(0,0)向右为x正坐标,向下为y正坐标 imagecopyresampled($dst_resource, $img_resource, $new_x, $new_y, $old_x, $old_y, $new_width, $new_height, $old_width, $old_height); $dst_resource: 背景图资源 $img_resource: 水印图资源 $new_x: 水印图平铺横坐标 $new_y: 水印图平铺纵坐标 $old_x: 水印图取样横坐标 $old_y: 水印图取样纵坐标 $new_width: 水印图平铺宽度 $new_height: 水印图平铺高度 $old_width: 水印图取样宽度 $old_height: 水印图取样高度 # $new_x $new_y 决定水印图相对背景图左上角的位置 # $old_x $old_y 决定水印图原本的左上角的原点定位 # $new_width $new_height 用来缩放水印图现在的长宽 # $old_width $old_height 用来声明水印图原本的长宽imagejpeg 同功能函数
imagepng imagegif imagewbmp imagewebpimagedestroy释放资源