求一函数,使图片缩小后图片看上去没变化,请多帮忙 我实现了一个图片缩放功能,可是缩小后的图片很难看,该怎么办?就是好看点 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 客户端缩放还是服务器端?如果用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);?> 我是在服务器端存的,可是图片缩小后失真,我也用imagecopyresampled可没什么起色,请多帮忙。 先试一下楼上的那个,觉得还没满足需求的话可以google keyword="php thumb class"肯定有很多 楼上的我试了有问题就是会从 $width ) { $height = ( $width * $height_orig ) / $width_orig; } else { $width = $width_orig; $height = $height_orig; } // 缩放 $image_p = imagecreatetruecolor( $width, $height );显示出来没有结果 zend studio 默认字符编码问题,新建php页默认gbk如何改成UTF8? 关于变量的一个小问题! 怎么把上传了的图片改名啊 ? 再问,checkbox在PHP下使用的方法! 算法探讨! up者有分! php.ini的设置问题 急急急,还是验证是否已经登陆的问题 急救,帮忙解释个函数!谢谢谢谢谢谢了 新手求助!! 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 );
显示出来
没有结果