$strSize=@getimagesize($strUrl); // @是让系统不会报错,要不然,$strUrl 的地址不对是会报告找不到图片的错误信息的。
$strWidth=$strSize[0];
$strHeight=$strSize[1];
也可以 print_r($strSize); 就都明白了。其中 $strUrl 是绝对地址,硬盘上的相对地址。 例如:$strUrl ="/home/web/user/aabb.jpg";
会了吗?
$strWidth=$strSize[0];
$strHeight=$strSize[1];
也可以 print_r($strSize); 就都明白了。其中 $strUrl 是绝对地址,硬盘上的相对地址。 例如:$strUrl ="/home/web/user/aabb.jpg";
会了吗?
{
$rate=$strWidth/300;
$strWidth=300;
$strHeight=$strHeight/$rate;
}if($strWidth != "" ) $strWidth = " width='".$strWidth."' ";
if($strHeight != "" ) $strHeight = " height='".$strHeight."' ";echo "<img src='".$strLongUrl."' border=0 ".$strWidth.$strHeight." >";
imagecopyresized()第一个效果比较好
以前写的一个。/**
* 等比例调整图片大小
*
* @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;
}
MARK
看有米高手写好的类或函数