我实现了一个图片缩放功能,可是缩小后的图片很难看,该怎么办?就是好看点
解决方案 »
- 有关php手册中的"counter" 扩展?
- phpmailer 发邮件问题 , 服务器连接不上
- 我这个发帖限制的程序代码有错吗?
- 求助:gettext实现网站国际化语言,当apache线程多时setlocale会出错
- 刷新重复提交数据
- 怎么传两个值啊
- php有timer功能吗??很奇怪?
- php中,dom解析xml文件,乱码问题
- Apache2+PHP+Zend+phpMyAdmin的整合安装包刚做好,需要一个空间存放,谁能提供?
- 哥们兄弟来了公司要用PHP做开发了唉来者有分送
- 郁闷哦!我实在是找不到错误在什么地方了哦!请大家帮忙啊!
- 我在php中写了图片缩放,怎么老出问题? 谁帮忙看看!我是新手!
如果用gd的话,想漂亮最好重采样
imagecopyresampled -- 重采样拷贝部分图像并调整大小如果有一定美工基础的话,不妨考虑自设滤镜和添加边框背景之类
/**
* 等比例调整图片大小
*
* @param $ 原始路径 $path_from
* @param $ 存放路径 $path_to
* @param $ 原有文件名 $name
* @param $ 新文件名 $outname
* @param $ 新宽度 $width
* @param $ 新高度 $height
* @return string 新文件名
*/
function resizeimg( $path_from, $path_to, $name , $outname, $width, $height ) {
if ( !$path_from or !$path_to or !$name or !$outname or !$width or !$height ) {
return false;
}
// 新文件名
$outputfile = $path_to . $outname;
if ( is_file ( $outputfile ) ) {
return false;
}
// 指定jpg图片压缩率
$compress = "90";
// 水印开关
$water = 0; //0 无水印 1 文字水印 2 图片水印 3 所有水印
// $waterposition=1; //水印位置(1为左下角,2为右下角,3为左上角,4为右上角,5为居中);
$waterstring = ""; //水印字符串
$font = 'simsun.ttc'; //文字字体
$size = 10; //文字水印大小
$waterimg = ""; //水印图片
$alpha = 40; $file_orig = $name;
// 获得原始文件名和分辨率
$pic_url_orig = $path_from . $file_orig;
list( $width_orig, $height_orig ) = getimagesize( $pic_url_orig );
// 计算新分辨率
if ( $width_orig > $width ) {
$height = ( $width * $height_orig ) / $width_orig;
} else {
$width = $width_orig;
$height = $height_orig;
}
// 缩放
$image_p = imagecreatetruecolor( $width, $height );
$type = getimagesize ( $pic_url_orig );
switch ( $type[2] ) {
case 1:
$simage = imagecreatefromgif( $pic_url_orig );
break;
case 2:
$simage = imagecreatefromjpeg( $pic_url_orig );
break;
case 3:
$simage = imagecreatefrompng( $pic_url_orig );
break;
case 6:
$simage = imagecreatefromwbmp( $pic_url_orig );
break;
default:
$simage = imagecreatefromjpeg( $pic_url_orig );
}
imagecopyresampled( $image_p, $simage, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig ); if ( $water == 1 or $water == 3 ) {
// 指定颜色
$black = imagecolorallocate( $image_p, 0, 0, 0 );
$grey = imagecolorallocate( $image_p, 192, 192, 192 );
$white = imagecolorexactalpha ( $image_p, 255, 255, 255, 30 );
$waterstring = $lang[waterstring];
$waterimg = $lang[waterimg];
// 填充背景色块 左上为原点 左上 至 右下
imagefilledrectangle( $image_p, 4, $height - $size - 7, 4 + $size * strlen( $waterstring ) / 1.5 , $height - 3, $white );
// 加水印字符串
// 添加阴影 左上为原点 字符的基线左下角坐标
// imagettftext( $image_p, $size, 0, 6, $height-4, $grey, $font, iconv("gb2312","utf-8", $waterstring ) );
imagettftext( $image_p, $size, 0, 6, $height-4, $grey, $font, mb_convert_encoding( $waterstring, "UTF-8", "GB2312" ) );
imagettftext( $image_p, $size, 0, 5, $height-5, $black, $font, mb_convert_encoding( $waterstring, "UTF-8", "GB2312" ) );
}
if ( $water == 2 or $water == 3 ) {
$typewater = getimagesize ( $waterimg );
// 加水印图片
switch ( $typewater[2] ) {
case 1:
$simage1 = imagecreatefromgif( $waterimg );
break;
case 2:
$simage1 = imagecreatefromjpeg( $waterimg );
break;
case 3:
$simage1 = imagecreatefrompng( $waterimg );
break;
case 6:
$simage1 = imagecreatefromwbmp( $waterimg );
break;
default:
$simage1 = imagecreatefromjpeg( $waterimg );
}
imagecopymerge( $image_p, $simage1, 0, 0, 0, 0, $typewater[0], $typewater[1] , $alpha );
imagedestroy( $simage1 );
}
// 输出
switch ( $type[2] ) {
case 1:
imagegif( $image_p, $outputfile );
break;
case 2:
imagejpeg( $image_p, $outputfile , $compress );
break;
case 3:
imagepng( $image_p, $outputfile );
break;
case 6:
imagewbmp( $image_p, $outputfile );
break;
default:
imagejpeg( $image_p, $outputfile , $compress );
}
return $outname;
}
$path=str_replace('test.php', '', str_replace('\\', '/', __FILE__));
$aa=rand(1000,9999).".jpg";
resizeimg($path."img/",$path,"d.jpg",$aa,105,80);
?>
肯定有很多
有问题就是会从 $width ) {
$height = ( $width * $height_orig ) / $width_orig;
} else {
$width = $width_orig;
$height = $height_orig;
}
// 缩放
$image_p = imagecreatetruecolor( $width, $height );
显示出来
没有结果