当前位置:首页 » 编程语言 » php图片缩放比例

php图片缩放比例

发布时间: 2022-04-25 17:03:45

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

可以。

等比例缩放的方法是:

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

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

2. php如何实时缩小图片大小

PHP中缩放图像:

有两种改变图像大小的方法.

(1):ImageCopyResized() 函数在所有GD版本中有效,但其缩放图像的算法比较粗糙.

(2):ImageCopyResampled(),其像素插值算法得到的图像边缘比较平滑.质量较好(但该函数的速度比
ImageCopyResized() 慢).

两个函数的参数是一样的.如下:

ImageCopyResampled(dest,src,dx,dy,sx,sy,dw,dh,sw,sh);

ImageCopyResized(dest,src,dx,dy,sx,sy,dw,dh,sw,sh);

它们两个都是从原图像(source)中抓取特定位置(sx,sy)复制图像qu区域到目标t
图像(destination)的特定位置(dx,dy)。另外dw,dh指定复制的图像区域在目标图像上的大小,sw,sh指定从原图像复制的图像区域
的大小。如果有ps经验的话,就相当于在原图像选择一块区域,剪切移动到目的图像上,同时有拉伸或缩小的操作。

例一:

(本例子是将图片按原大小的4/1的大小显示)

<?php
// 指定文件路径和缩放比例
$filename = 'test.jpg';
$percent = 0.5;
// 指定头文件Content type值
header('Content-type: image/jpeg');
// 获取图片的宽高
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;
// 创建一个图片。接收参数分别为宽高,返回生成的资源句柄
$thumb = imagecreatetruecolor($newwidth, $newheight);
//获取源文件资源句柄。接收参数为图片路径,返回句柄
$source = imagecreatefromjpeg($filename);
// 将源文件剪切全部域并缩小放到目标图片上。前两个为资源句柄
imageresampled($thumb, $source, 0, 0, 0, 0, $newwidth,
$newheight, $width, $height);
// 输出给浏览器
imagejpeg($thumb);
?>

3. 如何实现PHP图片裁剪与缩放

给你段代码吧。上边的是具体的事务处理。下面的是类文件。
////////////////////////////////////////////////////下面开始处理图片压缩问题
$src = "$fileurl";

echo $src;
$image = new Image($src);
$width= $image->getimgwidth();
echo $width."宽度是这些";

if($width>1024){

$coefficient=number_format(1024/$width, 4, '.', '');
echo $coefficient;
$image->percent = $coefficient;
$image->openImage();
$image->thumpImage();
//************************************重新给图片命名
$randname=date("Y").date("m").date("d").date("H").date("i").date("s").rand(100, 999).".".$hz;

echo "<br/>重新命名是这个".$randname;

$fileurl=$fileimgweb.$randname;//重新给数据库里存的图片地址命名
// $image->showImage();
$image->saveImage($fileurl);

}

////////////////////////////////////////////////////图片压缩问题处理结束

--------------------------------------------------------------------------------------

<?php
/**
图片压缩操作类
v1.0
*/
class Image{

private $src;
private $imageinfo;
private $image;
public $percent = 0.5;
public function __construct($src){

$this->src = $src;

}
/**
获取图片的宽度并传输给前台
*/

public function getimgwidth(){

$imgwidth= getimagesize($this->src)[0];
// echo $imgwidth;
return $imgwidth;

}

/**
打开图片
*/
public function openImage(){

list($width, $height, $type, $attr) = getimagesize($this->src);
$this->imageinfo = array(

'width'=>$width,
'height'=>$height,
'type'=>image_type_to_extension($type,false),
'attr'=>$attr
);
$fun = "imagecreatefrom".$this->imageinfo['type'];
$this->image = $fun($this->src);

}
/**
操作图片
*/
public function thumpImage(){

$new_width = $this->imageinfo['width'] * $this->percent;
$new_height = $this->imageinfo['height'] * $this->percent;
$image_thump = imagecreatetruecolor($new_width,$new_height);
//将原图复制带图片载体上面,并且按照一定比例压缩,极大的保持了清晰度
imageresampled($image_thump,$this->image,0,0,0,0,$new_width,$new_height,$this->imageinfo['width'],$this->imageinfo['height']);
imagedestroy($this->image);
$this->image = $image_thump;
}
/**
输出图片
*/
public function showImage(){

header('Content-Type: image/'.$this->imageinfo['type']);
$funcs = "image".$this->imageinfo['type'];
$funcs($this->image);

}
/**
保存图片到硬盘
*/
public function saveImage($fileurl){

imagejpeg($this->image, $fileurl,75);

// $funcs = "image".$this->imageinfo['type'];
// $funcs($this->image,$name.'.'.$this->imageinfo['type']);

}
/**
销毁图片
*/
public function destruct(){

imagedestroy($this->image);
}

}

?>

4. php实现图片等比例缩放代码

新建文件index.php,需要在统计目录下有个图片为q.jpg(可根据源码进行更改图片的名称)
源代码如下:
<?php
$filename="q.jpg";
$per=0.3;
list($width,
$height)=getimagesize($filename);
$n_w=$width*$per;
$n_h=$height*$per;
$new=imagecreatetruecolor($n_w,
$n_h);
$img=imagecreatefromjpeg($filename);
//拷贝部分图像并调整
imageresized($new,
$img,0,
0,0,
0,$n_w,
$n_h,
$width,
$height);
//图像输出新图片、另存为
imagejpeg($new,
"q1.jpg");
imagedestroy($new);
imagedestroy($img);
?>
使用浏览器运行过后,在index.php同级的目录下会有个q1.jpg,这个图片就是等比例缩放后的图片,路径可以自己在源代码里面更改,放在自己的项目当中去或写个方法也行
以上所述上就是本文的全部内容了,希望对大家学习php语言能够有所帮助。

5. HTML PHP 网页如何设定图片宽度超过700px 则自动缩小

第一个方法:

1、用css来实现IE7以上浏览器的图片缩略效果。

.divimg{ max-width:50px; max-height:50px; } *html.divimg{width:expression(this.width>50&&this.width>this.height?50:auto); height:expresion(this.height>50?50:auto); }

说明: 这段代码是把图片等比例缩小为50px * 50px,可以根据网页的宽度来自由修改尺寸。

2、万恶的IE6不识别上面的代码怎么办?

如果你的网页宽度为580像素,我们来看

.ltop3 { line-height: 25px; font-size: 12px; overflow: hidden; width: 580px;}

这样的话,IE7会识别第一步的代码来自动缩略,而IE6会根据第二步的代码,如果图片内容超过网页宽度就自动隐藏,这就完美的解决了图片的缩略问题。

另外还有一个方法也告诉大家,希望有用。

第2个方法:

JS代码加到网页<BODY>前面

<SCRIPT language=javaScript type=text/JavaScript>
//改变图片大小
function resizepic(thispic)
{
if(thispic.width>400) thispic.width=400;
}
//无级缩放图片大小
function bbimg(o)
{
var zoom=parseInt(o.style.zoom, 10)||100;
zoom+=event.wheelDelta/12;
if (zoom>0) o.style.zoom=zoom+'%';
return false;
}
</SCRIPT>

在图片属性加上onmousewheel="return bbimg(this)" onload="javascript:resizepic(this)" 代码即可让超过400像素的图片自动缩略。

6. php等比缩放图片,就是只按宽度缩小图片,当图片宽度大于750时就缩小到750 高度不用管 跟着宽度缩就行了

首先说一下思路,首先你要判断图片的宽度,这需要用到一个函数,个人比较喜欢用getimagesize()
其次是等比例绽放,需要用到imageresized(当然还有其他函数)
注意:我这里用到的是gd库
实现:
写一个函数或者类都行,我这里就以面向过程的方式来写,你可以整理一下
$file = 'pic.jpg'; //原图片文件
$maxWidth = 750;
$info = getimagesize($file); //取得一个图片信息的数组,索引 0 包含图像宽度的像素值,索引 1 包含图像高度的像素值。索引 2 是图像类型的标记
if($info[0] > $maxWidth )
{
exit('图片小于'.$maxWidth.',不需要缩放');
}
$im = imagecreatefromjpeg($file); //根据图片的格式对应的不同的函数,在此不多赘述。
$rate = $maxWidth/$info[0]; //计算绽放比例
$maxHeight = floor($info[1]*$rate); //计算出缩放后的高度
$des_im = imagecreatetruecolor($maxWidth,$maxHeight); //创建一个缩放的画布
imageresized($des_im,$im,0,0,0,0,$maxWidth,$maxHeight,$info[0],$info[1]); //缩放
imagejpeg($des_im,'thumb.jpg'); //输出到thumb.jpg即为一个缩放后的文件

7. 跪求PHP上传图片并按比例缩放到一定尺寸的程序,非常感谢

publicfunctionupimges(){
$filename=time();
header("Content-Type:text/html;charset=utf-8");
$upload=newUpimage('','','','',$filename);
//设置上传文件大小
$upload->maxSize=100000000000000;
//$upload->saveRule=microtime().'_'.mt_rand(1000,9999);
//上传文件命名规则timeuniqidcom_create_guid等
//$upload->$saveRule=time();
//设置上传文件类型
$upload->allowExts=explode(',','jpg,gif,png,jpeg');
//设置附件上传目录
$upload->savePath='./data/attached/skp/'.$_SESSION['formtoken'].'/';
$upload->thumb=true;
//设置引用图片类库包路径
$upload->imageClassPath='./Image.php';
//设置需要生成缩略图的文件后缀
$upload->thumbPrefix='thumb_,thumbm_,thumbl_,thumbs_';//生产4张缩略图
//设置缩略图最大宽度
$upload->thumbMaxWidth='960,480,120,64';
//设置缩略图最大高度
$upload->thumbMaxHeight='960,480,120,64';
//删除原图
$upload->thumbRemoveOrigin=false;

if(!$upload->upload()){
$data['tip']=$upload->getErrorMsg();
$data['status']='0';
}else{
$info=$upload->getUploadFileInfo();

$data['counts']=count($info);
$data['status']='1';
$data['tip']='上传成功';
$data['info']=$info;
}

echojson_encode($data);
}

这个是代码和所引用的

8. 如何实现php图片等比例缩放

$imgsrc=$image_name;
$imgdst=$image_name;
list($width,$height,$type)=getimagesize($imgsrc);
$new_width=($width>600?600:$width)*0.9;
$new_height=($height>600?600:$height)*0.9;
$giftype=check_gifcartoon($imgsrc);
$image_wp=imagecreatetruecolor($new_width,$new_height);
$image=imagecreatefromgif($imgsrc);
imageresampled($image_wp,$image,0,0,0,0,$new_width,$new_height,$width,$height);
imagejpeg($image_wp,$imgdst,75);
imagedestroy($image_wp);

9. 求php图片缩放处理函数

<?php
/**
*图片缩放
*@paramstring$url
*@paramint$maxWidth
*@paramint$maxHeight
*@returnstring
*/
functionthumb($url,$maxWidth,$maxHeight,&$info){
$info=$imgInfo=getimagesize($url);
$width=$imgInfo[0];//获取图片宽度
$height=$imgInfo[1];//获取图片高度
$r=min($maxHeight/$height,$maxWidth/$width);
if($r>=1){//不用缩放
$maxHeight=$height;
$maxWidth=$width;
}elseif($r<1){//缩放
$maxHeight=$height*$r;
$maxWidth=$width*$r;
}
$temp_img=imagecreatetruecolor($maxWidth,$maxHeight);//创建画布
$fun=str_replace('/','createfrom',$imgInfo['mime']);
$im=$fun($url);
imageresized($temp_img,$im,0,0,0,0,$maxWidth,$maxHeight,$width,$height);

ob_start();
$fun=str_replace('/','',$imgInfo['mime']);
$fun($temp_img);
$imgstr=ob_get_contents();
ob_end_clean();
imagedestroy($im);
return$imgstr;
}

$imgUrl=$_GET['url'];
$info=array();
$string=thumb($imgUrl,500,500,$info);
$mimeArray=explode("/",$info['mime']);
header("Content-Type:image/{$mimeArray[1]}");
echo$string;

以上代码存为thumb.php,调用效果:

10. php 怎么实现不等比例缩放图片

functiontep_image($src,$alt='',$width='',$height='',$parameters=''){

$image='<imgsrc="'.tep_output_string($src).'"alt="'.$alt.'"';

if(tep_not_null($alt)){
$image.='title="'.$alt.'"';
}

if(((empty($width)||empty($height))){
if($image_size=@getimagesize($src)){
if(empty($width)&&tep_not_null($height)){
$ratio=$height/$image_size[1];
$width=intval($image_size[0]*$ratio);
}elseif(tep_not_null($width)&&empty($height)){
$ratio=$width/$image_size[0];
$height=intval($image_size[1]*$ratio);
}elseif(empty($width)&&empty($height)){
$width=$image_size[0];
$height=$image_size[1];
}
}
}

if(tep_not_null($width)&&tep_not_null($height)){
$image.='width="'.$width.'"height="'.$height.'"';
}

if(tep_not_null($parameters))$image.=''.$parameters;

$image.='/>';

return$image;
}
functiontep_not_null($value){
if(is_array($value)){
if(sizeof($value)>0){
returntrue;
}else{
returnfalse;
}
}else{
if(($value!='')&&(strtolower($value)!='null')&&(strlen(trim($value))>0)){
returntrue;
}else{
returnfalse;
}
}
}

用tep_image('图片路径','图片alt','width','height');

宽高自己设定 就可以自动等比例缩放了

热点内容
编译选项立即绑定未定义符号 发布:2025-05-16 20:55:13 浏览:905
linuxmysql慢日志 发布:2025-05-16 20:47:58 浏览:270
村两委有哪些配置 发布:2025-05-16 20:34:47 浏览:292
我的世界有什么服务器好玩的 发布:2025-05-16 20:28:57 浏览:482
c语言按位与运算 发布:2025-05-16 20:24:10 浏览:753
苹果手机如何修改密码安全 发布:2025-05-16 20:23:34 浏览:193
图片文字识别算法 发布:2025-05-16 20:21:54 浏览:45
校园ftp服务器 发布:2025-05-16 20:19:38 浏览:71
数据加密技术的实现 发布:2025-05-16 20:12:49 浏览:158
华为p9扩存储 发布:2025-05-16 20:03:22 浏览:414