好得很程序员自学网
  • 首页
  • 后端语言
    • C#
    • PHP
    • Python
    • java
    • Golang
    • ASP.NET
  • 前端开发
    • Angular
    • react框架
    • LayUi开发
    • javascript
    • HTML与HTML5
    • CSS与CSS3
    • jQuery
    • Bootstrap
    • NodeJS
    • Vue与小程序技术
    • Photoshop
  • 数据库技术
    • MSSQL
    • MYSQL
    • Redis
    • MongoDB
    • Oracle
    • PostgreSQL
    • Sqlite
    • 数据库基础
    • 数据库排错
  • CMS系统
    • HDHCMS
    • WordPress
    • Dedecms
    • PhpCms
    • 帝国CMS
    • ThinkPHP
    • Discuz
    • ZBlog
    • ECSHOP
  • 高手进阶
    • Android技术
    • 正则表达式
    • 数据结构与算法
  • 系统运维
    • Windows
    • apache
    • 服务器排错
    • 网站安全
    • nginx
    • linux系统
    • MacOS
  • 学习教程
    • 前端脚本教程
    • HTML与CSS 教程
    • 脚本语言教程
    • 数据库教程
    • 应用系统教程
  • 新技术
  • 编程导航
    • 区块链
    • IT资讯
    • 设计灵感
    • 建站资源
    • 开发团队
    • 程序社区
    • 图标图库
    • 图形动效
    • IDE环境
    • 在线工具
    • 调试测试
    • Node开发
    • 游戏框架
    • CSS库
    • Jquery插件
    • Js插件
    • Web框架
    • 移动端框架
    • 模块管理
    • 开发社区
    • 在线课堂
    • 框架类库
    • 项目托管
    • 云服务

当前位置:首页>后端语言>PHP
<tfoot draggable='sEl'></tfoot>

php自动缩图 php怎么设置图片的大小

很多站长朋友们都不太清楚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.com 希望能共同讨论第三种功能,裁剪指定部分! 呵呵~ :) have fun~

关于php自动缩图的介绍到此就结束了,不知道本篇文章是否对您有帮助呢?如果你还想了解更多此类信息,记得收藏关注本站,我们会不定期更新哦。

查看更多关于php自动缩图 php怎么设置图片的大小的详细内容...

声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did210049
更新时间:2023-05-03   阅读:23次

上一篇: php移动文件的返回 php返回信息

下一篇:phpexcel示例 php excel

相关资讯

最新资料更新

  • 1.关于winformphp的信息
  • 2.怎么开发一个php项目 php项目开发流程
  • 3.phphtml补全 html自动补全代码
  • 4.php项目补充redis php+redis
  • 5.php网络通信 php通信协议
  • 6.朔州php后台开发 朔州php培训招聘
  • 7.接口的继承php 接口的继承和实现
  • 8.php类属性分类 php数据类型
  • 9.proxy.php proxyphp?url
  • 10.php查询数组找到 php查找数组元素
  • 11.php订单管理功能 php订单提醒功能
  • 12.搭建分站源码php 建立分站怎么建
  • 13.phpcrc-itu的简单介绍
  • 14.php项目基本流程 php项目如何运行
  • 15.php图片上传及显示 php 图片上传
  • 16.hbuilder写php hbuilder写PHP
  • 17.php异步协程 php yield 异步
  • 18.php推荐接口排重 接口 php
  • 19.php教程全局交流 phpunit教程
  • 20.phphuoqv多选 php选择语句

CopyRight:2016-2025好得很程序员自学网 备案ICP:湘ICP备09009000号-16 http://haodehen.cn
本站资讯不构成任何建议,仅限于个人分享,参考须谨慎!
本网站对有关资料所引致的错误、不确或遗漏,概不负任何法律责任。
本网站刊载的所有内容(包括但不仅限文字、图片、LOGO、音频、视频、软件、程序等)版权归原作者所有。任何单位或个人认为本网站中的内容可能涉嫌侵犯其知识产权或存在不实内容时,请及时通知本站,予以删除。

网站内容来源于网络分享,如有侵权发邮箱到:kenbest@126.com,收到邮件我们会即时下线处理。
网站框架支持:HDHCMS   51LA统计 百度统计
Copyright © 2018-2025 「好得很程序员自学网」
[ SiteMap ]