php数据缩图代码,php 图片缩放

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

新建文件index.php,需要在统计目录下有个图片为q.jpg(可根据源码进行更改图片的名称)

创新互联-专业网站定制、快速模板网站建设、高性价比阿勒泰网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式阿勒泰网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖阿勒泰地区。费用合理售后完善,10多年实体公司更值得信赖。

源代码如下:

?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);

//拷贝部分图像并调整

imagecopyresized($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语言能够有所帮助。

PHP等比例压缩图片的实例代码

具体代码如下所示:

/**

*

desription

压缩图片

*

@param

sting

$imgsrc

图片路径

*

@param

string

$imgdst

压缩后保存路径

*/

public

function

compressedImage($imgsrc,

$imgdst)

{

list($width,

$height,

$type)

=

getimagesize($imgsrc);

$new_width

=

$width;//压缩后的图片宽

$new_height

=

$height;//压缩后的图片高

if($width

=

600){

$per

=

600

/

$width;//计算比例

$new_width

=

$width

*

$per;

$new_height

=

$height

*

$per;

}

switch

($type)

{

case

1:

$giftype

=

check_gifcartoon($imgsrc);

if

($giftype)

{

header('Content-Type:image/gif');

$image_wp

=

imagecreatetruecolor($new_width,

$new_height);

$image

=

imagecreatefromgif($imgsrc);

imagecopyresampled($image_wp,

$image,

0,

0,

0,

0,

$new_width,

$new_height,

$width,

$height);

//90代表的是质量、压缩图片容量大小

imagejpeg($image_wp,

$imgdst,

90);

imagedestroy($image_wp);

imagedestroy($image);

}

break;

case

2:

header('Content-Type:image/jpeg');

$image_wp

=

imagecreatetruecolor($new_width,

$new_height);

$image

=

imagecreatefromjpeg($imgsrc);

imagecopyresampled($image_wp,

$image,

0,

0,

0,

0,

$new_width,

$new_height,

$width,

$height);

//90代表的是质量、压缩图片容量大小

imagejpeg($image_wp,

$imgdst,

90);

imagedestroy($image_wp);

imagedestroy($image);

break;

case

3:

header('Content-Type:image/png');

$image_wp

=

imagecreatetruecolor($new_width,

$new_height);

$image

=

imagecreatefrompng($imgsrc);

imagecopyresampled($image_wp,

$image,

0,

0,

0,

0,

$new_width,

$new_height,

$width,

$height);

//90代表的是质量、压缩图片容量大小

imagejpeg($image_wp,

$imgdst,

90);

imagedestroy($image_wp);

imagedestroy($image);

break;

}

}

总结

以上所述是小编给大家介绍的PHP等比例压缩图片的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

您可能感兴趣的文章:php中10个不同等级压缩优化图片操作示例PHP

实现等比压缩图片尺寸和大小实例代码php

gd等比例缩放压缩图片函数基于PHP实现等比压缩图片大小php上传图片并压缩的实现方法PHP实现图片上传并压缩PHP实现图片压缩的两则实例php使用imagick模块实现图片缩放、裁剪、压缩示例

PHP网站上传图片自动压缩,怎么编程啊,求指

这里会使用到三个文件:

connect.php:连接数据库

test_upload.php:执行SQL语句

upload_img.php:上传图片并压缩

三个文件代码如下:

连接数据库:connect.php

?php

$db_host = '';

$db_user = '';

$db_psw = '';

$db_name = '';

$db_port = '';

$sqlconn=new mysqli($db_host,$db_user,$db_psw,$db_name);

$q="set names utf8;";

$result=$sqlconn-query($q);

if (mysqli_connect_errno()) {

printf("Connect failed: %s\n", mysqli_connect_error());

exit();

}

?

当然使用一些封装的数据库类也是可以的。

执行SQL语句:test_upload.php

?php

require ("connect.php");

require ("upload_img.php");

$real_img=$uploadfile; 

$small_img=$uploadfile_resize;

$insert_sql = "insert into img (real_img,small_img) values (?,?)";

$result = $sqlconn - prepare($insert_sql);

$result - bind_param("ss", $real_img,$small_img);

$result - execute();

?

上传图片并压缩:upload_img.php

?php 

//设置文件保存目录

$uploaddir = "upfiles/"; 

//设置允许上传文件的类型

$type=array("jpg","gif","bmp","jpeg","png"); 

//获取文件后缀名函数 

function fileext($filename) 

return substr(strrchr($filename, '.'), 1); 

//生成随机文件名函数 

function random($length) 

$hash = 'CR-'; 

$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz'; 

$max = strlen($chars) - 1; 

mt_srand((double)microtime() * 1000000); 

for($i = 0; $i  $length; $i++) 

$hash .= $chars[mt_rand(0, $max)]; 

return $hash; 

$a=strtolower(fileext($_FILES['filename']['name'])); 

//判断文件类型 

if(!in_array(strtolower(fileext($_FILES['filename']['name'])),$type)) 

$text=implode(",",$type); 

$ret_code=3;//文件类型错误

$page_result=$text;

$retArray = array('ret_code' = $ret_code,'page_result'=$page_result);

$retJson = json_encode($retArray);

echo $retJson;

return;

//生成目标文件的文件名 

else

$filename=explode(".",$_FILES['filename']['name']); 

do

$filename[0]=random(10); //设置随机数长度 

$name=implode(".",$filename); 

//$name1=$name.".Mcncc"; 

$uploadfile=$uploaddir.$name; 

while(file_exists($uploadfile)); 

if (move_uploaded_file($_FILES['filename']['tmp_name'],$uploadfile)) 

if(is_uploaded_file($_FILES['filename']['tmp_name'])) 

{

$ret_code=1;//上传失败

else

{//上传成功

$ret_code=0;

$retArray = array('ret_code' = $ret_code);

$retJson = json_encode($retArray);

echo $retJson;

}

//压缩图片

$uploaddir_resize="upfiles_resize/";

$uploadfile_resize=$uploaddir_resize.$name;

//$pic_width_max=120;

//$pic_height_max=90;

//以上与下面段注释可以联合使用,可以使图片根据计算出来的比例压缩

$file_type=$_FILES["filename"]['type'];

function ResizeImage($uploadfile,$maxwidth,$maxheight,$name)

{

//取得当前图片大小

$width = imagesx($uploadfile);

$height = imagesy($uploadfile);

$i=0.5;

//生成缩略图的大小

if(($width  $maxwidth) || ($height  $maxheight))

{

/*

$widthratio = $maxwidth/$width;

$heightratio = $maxheight/$height;

if($widthratio  $heightratio)

{

$ratio = $widthratio;

}

else

{

$ratio = $heightratio;

}

$newwidth = $width * $ratio;

$newheight = $height * $ratio;

*/

$newwidth = $width * $i;

$newheight = $height * $i;

if(function_exists("imagecopyresampled"))

{

$uploaddir_resize = imagecreatetruecolor($newwidth, $newheight);

imagecopyresampled($uploaddir_resize, $uploadfile, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

}

else

{

$uploaddir_resize = imagecreate($newwidth, $newheight);

imagecopyresized($uploaddir_resize, $uploadfile, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

}

ImageJpeg ($uploaddir_resize,$name);

ImageDestroy ($uploaddir_resize);

}

else

{

ImageJpeg ($uploadfile,$name);

}

}

if($_FILES["filename"]['size'])

{

if($file_type == "image/pjpeg"||$file_type == "image/jpg"|$file_type == "image/jpeg")

{

//$im = imagecreatefromjpeg($_FILES[$upload_input_name]['tmp_name']);

$im = imagecreatefromjpeg($uploadfile);

}

elseif($file_type == "image/x-png")

{

//$im = imagecreatefrompng($_FILES[$upload_input_name]['tmp_name']);

$im = imagecreatefromjpeg($uploadfile);

}

elseif($file_type == "image/gif")

{

//$im = imagecreatefromgif($_FILES[$upload_input_name]['tmp_name']);

$im = imagecreatefromjpeg($uploadfile);

}

else//默认jpg

{

$im = imagecreatefromjpeg($uploadfile);

}

if($im)

{

ResizeImage($im,$pic_width_max,$pic_height_max,$uploadfile_resize);

ImageDestroy ($im);

}

?

请按照现实情况更改connect.php,test_upload.php中对应的信息。

望采纳,谢谢。

当前标题:php数据缩图代码,php 图片缩放
本文地址:https://www.cdcxhl.com/article24/dsisgje.html

成都网站建设公司_创新互联,为您提供小程序开发云服务器网页设计公司搜索引擎优化营销型网站建设

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都网站建设