很多站长朋友们都不太清楚php自动缩图,今天小编就来给大家整理php自动缩图,希望对各位有所帮助,具体内容如下:
本文目录一览: 1、 php如何实时缩小图片大小 2、 php 怎么实现不等比例缩放图片? 3、 php 怎么压缩图片的大小 4、 php中怎么做缩图和截图功能呢? 5、 PHP网站上传图片自动压缩,怎么编程啊,求指 6、 PHP5.0可以自动将图片缩放吗? 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 typezhi值
header('Content-type: image/jpeg');
// 获取图片的宽高
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;
// 创建一个图片。接收参数分别为宽高,返回生成的资源句柄
$thumb = imagecreatetruecolor($newwidth, $newheight);
//获取源文件资源句柄。接收参数为图片路径,返回句柄
$source = imagecreatefromjpeg($filename);
// 将源文件剪切全部域并缩小放到目标图片上。前两个为资源句柄
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth,
$newheight, $width, $height);
// 输出给浏览器
imagejpeg($thumb);
?>
php 怎么实现不等比例缩放图片?function?tep_image($src,?$alt?=?'',?$width?=?'',?$height?=?'',?$parameters?=?'')?{
????$image?=?'<img?src="'?.?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;
??}
function?tep_not_null($value)?{
????if?(is_array($value))?{
??????if?(sizeof($value)?>?0)?{
????????return?true;
??????}?else?{
????????return?false;
??????}
????}?else?{
??????if?(($value?!=?'')??(strtolower($value)?!=?'null')??(strlen(trim($value))?>?0))?{
????????return?true;
??????}?else?{
????????return?false;
??????}
????}
??}
用tep_image('图片路径','图片alt','width','height');
宽高自己设定 就可以自动等比例缩放了
php 怎么压缩图片的大小php?压缩图片的大小:
<?php
$im?=?imagecreatefromjpeg('D:phpplace.jpeg');
resizeImage($im,,,'xinde','.jpg');
function?resizeImage($im,$maxwidth,$maxheight,$name,$filetype)
{
$pic_width?=?imagesx($im);
$pic_height?=?imagesy($im);
echo?"start-----------------"?;
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,,,,,$newwidth,$newheight,$pic_width,$pic_height);
}
else
{
$newim?=?imagecreate($newwidth,$newheight);
imagecopyresized($newim,$im,,,,,$newwidth,$newheight,$pic_width,$pic_height);
}
$name?=?$name.$filetype;
imagejpeg($newim,$name);
imagedestroy($newim);
}
else
{
$name?=?$name.$filetype;
imagejpeg($im,$name);
}
}
php中怎么做缩图和截图功能呢?php中内置了相当多的跟图片处理相关的函数。要做缩图或者截图功能的话,可以参考php手册中的 imagecopyresampled 函数使用说明以及它的一个示例代码。大致思路就是:[*]打开原图;[*]创建一个空图;[*]按照指定大小和坐标从原图复制一部分或者全部到新的空图;[*]保存复制后的新图片; 查看原帖>>
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中对应的信息。
望采纳,谢谢。
PHP5.0可以自动将图片缩放吗?有,我最近刚用了这个程序,也是和你一样,对第三个功能有需求,正在研究中,呵呵~
程序如下:
<?php
//处理图像的函数
function createthumb($name,$filename,$new_w,$new_h){
$system=explode(".",$name);
if (preg_match("/gif/",$system[1])){$src_img=imagecreatefromgif($name);}
if (preg_match("/jpg|jpeg/",$system[1])){$src_img=imagecreatefromjpeg($name);}
if (preg_match("/png/",$system[1])){$src_img=imagecreatefrompng($name);}
$old_x=imageSX($src_img);
$old_y=imageSY($src_img);
if ($old_x > $old_y) {
$thumb_h=$new_h;
$thumb_w=$old_x*($new_h/$old_y);
}
if ($old_x < $old_y) {
$thumb_h=$old_y*($new_w/$old_x);
$thumb_w=$new_w;
}
if ($old_x == $old_y) {
$thumb_w=$new_w;
$thumb_h=$new_h;
}
$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
if (preg_match("/png/",$system[1])){
imagepng($dst_img,$filename);
} else {
imagejpeg($dst_img,$filename);
}
imagedestroy($dst_img);
imagedestroy($src_img);
echo "缩略图已生成,";
}
$uploadDir = "upload/";
$getime=time();
$uploadFile = $uploadDir.$_FILES['userfile']['name'];
$type=strtolower(substr(strrchr($uploadFile,"."),1));
if (move_uploaded_file($_FILES['userfile']['tmp_name'],$uploadFile))
{
$photos_name= $uploadDir.'thumb/'.$getime.'.'.$type;
createthumb($uploadFile,$photos_name,75,75);
echo "{$_FILES['userfile']['name']} 上传成功!";
}else{
echo "上传失败!";
}
?>
HTML部分 就是上传的表单部分:
<html>
<head>
<title></title>
</head>
<body>
<form enctype="multipart/form-data" action="upload.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="3000000">
Upload: <input name="userfile" type="file" value="" size="20"> <input type="submit" value="Send">
</form>
</body>
</html>
如果可能,我的MSN: yicernju@hotmail测试数据 希望能共同讨论第三种功能,裁剪指定部分! 呵呵~ :) have fun~
关于php自动缩图的介绍到此就结束了,不知道本篇文章是否对您有帮助呢?如果你还想了解更多此类信息,记得收藏关注本站,我们会不定期更新哦。
查看更多关于php自动缩图 php怎么设置图片的大小的详细内容...