php图片可以等比例的缩放吗

发布网友 发布时间:2022-04-22 02:46

我来回答

5个回答

热心网友 时间:2022-04-06 04:10

可以。

等比例缩放的方法是:

1、载入选区--自由变换。如下图:

2、按住shift+alt键,使用鼠标调整大小,这种情况下,选区会按照等比例的方法进行缩放的。

热心网友 时间:2022-04-06 05:28

理论上来说,任何图片都可以等比缩放,但是不能保证缩放后的清晰度最简单的方法是在CSS里面不设置图片的宽度,只设置高度就做到等比缩放了

热心网友 时间:2022-04-06 07:03

php图片等比例的缩放的方法:
1,上传地址随机生成,指向到不同的服务器,以达到每个服务器上传压力和存储负载均衡
2,上传服务器上有事件监听事件,有新文件上传,可以使用rsync等协议将文件推到其它服务器
3,通过CDN等方式在有需要的时候,将文件同步到其它服务器

热心网友 时间:2022-04-06 08:54

//原文件,新文件,宽度,高度,维持比例//大图地址,新的图片地址名称,默认大小400*400
function ResizeImage($big_image_name, $new_name, $max_width = 400, $max_height = 400, $resize = 1){
$returnr['file']='';
$returnr['filetype']='';
if($temp_img_type = @getimagesize($big_image_name)) {eregi('/([a-z]+)$', $temp_img_type[mime], $tpn); $img_type = $tpn[1];}
else {eregi('.([a-z]+)$', $big_image_name, $tpn); $img_type = $tpn[1];}
$all_type = array(
"jpg" => array("create"=>"ImageCreateFromjpeg", "output"=>"imagejpeg" , "exn"=>".jpg"),
"gif" => array("create"=>"ImageCreateFromGIF" , "output"=>"imagegif" , "exn"=>".gif"),
"jpeg" => array("create"=>"ImageCreateFromjpeg", "output"=>"imagejpeg" , "exn"=>".jpg"),
"png" => array("create"=>"imagecreatefrompng" , "output"=>"imagepng" , "exn"=>".png"),
"wbmp" => array("create"=>"imagecreatefromwbmp", "output"=>"image2wbmp" , "exn"=>".wbmp")
); $func_create = $all_type[$img_type]['create'];
if(empty($func_create) or !function_exists($func_create))
{
return $returnr;
}
//输出
$func_output = $all_type[$img_type]['output'];
$func_exname = $all_type[$img_type]['exn'];
if(($func_exname=='.gif'||$func_exname=='.png'||$func_exname=='.wbmp')&&!function_exists($func_output))
{
$func_output='imagejpeg';
$func_exname='.jpg';
}
$big_image = $func_create($big_image_name);
$big_width = imagesx($big_image);
$big_height = imagesy($big_image);
if($big_width <= $max_width and $big_height <= $max_height)
{
$func_output($big_image, $new_name.$func_exname);
$returnr['file']=$new_name.$func_exname;
$returnr['filetype']=$func_exname;
return $returnr;
}
$ratiow = $max_width / $big_width;
$ratioh = $max_height / $big_height;
if($resize == 1) {
if($big_width >= $max_width and $big_height >= $max_height)
{
if($big_width > $big_height)
{
$tempx = $max_width / $ratioh;
$tempy = $big_height;
$srcX = ($big_width - $tempx) / 2;
$srcY = 0;
} else {
$tempy = $max_height / $ratiow;
$tempx = $big_width;
$srcY = ($big_height - $tempy) / 2;
$srcX = 0;
}
} else {
if($big_width > $big_height)
{
$tempx = $max_width;
$tempy = $big_height;
$srcX = ($big_width - $tempx) / 2;
$srcY = 0;
} else {
$tempy = $max_height;
$tempx = $big_width;
$srcY = ($big_height - $tempy) / 2;
$srcX = 0;
}
}
} else {
$srcX = 0;
$srcY = 0;
$tempx = $big_width;
$tempy = $big_height;
} $new_width = ($ratiow > 1) ? $big_width : $max_width;
$new_height = ($ratioh > 1) ? $big_height : $max_height;
if(function_exists("imagecopyresampled"))
{
$temp_image = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($temp_image, $big_image, 0, 0, $srcX, $srcY, $new_width, $new_height, $tempx, $tempy);
} else {
$temp_image = imagecreate($new_width, $new_height);
imagecopyresized($temp_image, $big_image, 0, 0, $srcX, $srcY, $new_width, $new_height, $tempx, $tempy);
}
$func_output($temp_image, $new_name.$func_exname);
ImageDestroy($big_image);
ImageDestroy($temp_image);
$returnr['file']=$new_name.$func_exname;
$returnr['filetype']=$func_exname;
return $returnr;
}

热心网友 时间:2022-04-06 11:02

 在PHP网站开发过程中,如果你建立的网站涉及大量的图片处理,必然涉及到图片上传,缩放,而如何保持图片不失真,是很多初级PHP网站开发者比较头疼的一件事,今天David就和大家分享一下如何进行图片缩放。使用之前你需要下载安装GD库,以支持PHP图片处理。下面我们结合代码讲解具体的PHP图片缩放处理的思路。 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56function resizeImage($im,$maxwidth,$maxheight,$name,$filetype)
{
$pic_width = imagesx($im);
$pic_height = imagesy($im);

if(($maxwidth && $pic_width > $maxwidth) || ($maxheight && $pic_height > $maxheight))
{
if($maxwidth && $pic_width>$maxwidth)
{
$widthratio = $maxwidth/$pic_width;
$resizewidth_tag = true;
}

if($maxheight && $pic_height>$maxheight)
{
$heightratio = $maxheight/$pic_height;
$resizeheight_tag = true;
}

if($resizewidth_tag && $resizeheight_tag)
{
if($widthratio<$heightratio)
$ratio = $widthratio;
else
$ratio = $heightratio;
}

if($resizewidth_tag && !$resizeheight_tag)
$ratio = $widthratio;
if($resizeheight_tag && !$resizewidth_tag)
$ratio = $heightratio;

$newwidth = $pic_width * $ratio;
$newheight = $pic_height * $ratio;

if(function_exists("imagecopyresampled"))
{
$newim = imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);
}
else
{
$newim = imagecreate($newwidth,$newheight);
imagecopyresized($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);
}

$name = $name.$filetype;
imagejpeg($newim,$name);
imagedestroy($newim);
}
else
{
$name = $name.$filetype;
imagejpeg($im,$name);
}
}参数说明:$im 图片对象,应用函数之前,你需要用imagecreatefromjpeg()读取图片对象,如果PHP环境支持PNG,GIF,也可使用imagecreatefromgif(),imagecreatefrompng();$maxwidth 定义生成图片的最大宽度(单位:像素)$maxheight 生成图片的最大高度(单位:像素)$name 生成的图片名$filetype 最终生成的图片类型(.jpg/.png/.gif)代码注释:第3~4行:读取需要缩放的图片实际宽高第8~26行:通过计算实际图片宽高与需要生成图片的宽高的压缩比例最终得出进行图片缩放是根据宽度还是高度进行缩放,当前程序是根据宽度进行图片缩放。如果你想根据高度进行图片缩放,你可以将第22行的语句改成$widthratio>$heightratio第28~31行:如果实际图片的长度或者宽度小于规定生成图片的长度或者宽度,则要么根据长度进行图片缩放,要么根据宽度进行图片缩放。第33~34行:计算最终缩放生成的图片长宽。第36~45行:根据计算出的最终生成图片的长宽改变图片大小,有两种改变图片大小的方法:ImageCopyResized()函数在所有GD版本中有效,但其缩放图像的算法比较粗糙。ImageCopyResamples(),其像素插值算法得到的图像边缘比较平滑,但该函数的速度比ImageCopyResized()慢。第47~49行:最终生成经过处理后的图片,如果你需要生成GIF或PNG,你需要将imagejpeg()函数改成imagegif()或imagepng()第51~56行:如果实际图片的长宽小于规定生成的图片长宽,则保持图片原样,同理,如果你需要生成GIF或PNG,你需要将imagejpeg()函数改成imagegif()或imagepng()。特别说明:  GD库1.6.2版以前支持GIF格式,但因GIF格式使用LZW演算法牵涉专利权,因此在GD1.6.2版之后不支持GIF的格式。如果你是WINDOWS的环境,你只要进入PHP.INI文件找到extension=php_gd2.dll,将#去除,重启APACHE即可,如果你是Linux环境,又想支持GIF,PNG,JPEG,你需要去下载libpng,zlib,以及freetype字体并安装。  OK,PHP图片压缩函数完成,最后我们概述一下整个处理的思路:  通过计算实际图片的长宽与规定生成图片的长宽之间的缩放比例,根据实际的需求(按照宽度还是按照高度进行图片缩放)计算出最终生成图片的大小,然后应用PHP图片处理函数对图片进行处理,最后输出图片。  以上就是关于PHP图片处理中如何对图片进行压缩并保持不失真的函数说明,有疑问或者好的建议欢迎给我留言,下次我将分享在PHP网站开发建设完成后,由于图片目录没有规划好,我们该如何对图片进行迁移的思路。
声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。
E-MAIL:11247931@qq.com