search
Homephp教程php手册php 创建等比例图片代码

php 创建等比例图片代码

Jun 13, 2016 am 09:49 AM
phpcodetransfercreateandpicturemaximumProportion

说 明:$maxwidth和$maxheight只能传递一个,如果传最大宽度将自动计算高度,如果传最大高度将自动计算宽度 * 返 回 值:如果创建成功返回文件保存的地址,否则返回false

 说    明:$maxwidth和$maxheight只能传递一个,如果传最大宽度将自动计算高度,如果传最大高度将自动计算宽度
* 返 回 值:如果创建成功返回文件保存的地址,否则返回false

/************************************************************************
* 函数名称:createSmallImg()
* 函数说明:创建等比例图片
* 输入参数:
 $dir 保存路径
 $source_img 原图片名称
 $small_ex 缩率图文件名后缀
 $maxwidth 最大宽度
 $maxheight 最大高度
* 说    明:$maxwidth和$maxheight只能传递一个,如果传最大宽度将自动计算高度,如果传最大高度将自动计算宽度
* 返 回 值:如果创建成功返回文件保存的地址,否则返回false
* 编 写 者:李小宇
* 编写时间:2011/8/18
**************************************************************************/
function createSmallImg($dir,$source_img,$small_ex="_s",$maxwidth='',$maxheight='') {
 if(!empty($maxwidth) && !empty($maxheight)) {
  return false;
 }
 $img_name=substr($source_img,0,-4);
 $img_ex = strtolower(substr(strrchr($source_img,"."),1));
 /*注释的这段用作直接在浏览器上显示图片
 $im=imagecreatefromjpeg($file);
 header("Content-type: image/jpeg");
 imagejpeg($im);*/
 switch($img_ex) {
  case "jpg":
   $src_img=imagecreatefromjpeg($dir.$source_img);
   break;
  case "gif":
   $src_img=imagecreatefromgif($dir.$source_img);
   break;
  case "png":
   $src_img=imagecreatefrompng($dir.$source_img);
   break;
 }
 $old_width=imagesx($src_img);
 $old_height=imagesy($src_img);
 if(!empty($maxheight) && $old_height>=$maxheight) {
  $new_height=$maxheight;
  $new_width=round(($old_width*$new_height)/$old_height);
 } elseif(!empty($maxwidth) && $old_width>=$maxwidth) {
  $new_width=$maxwidth;
  $new_height=round(($old_height*$new_width)/$old_width);
 }
 if(!empty($new_width) || !empty($new_height)) {
  if($img_ex=="jpg" || $img_ex=="png") {
   $dst_img=imagecreatetruecolor($new_width,$new_height);
  } else {
   $dst_img=imagecreate($new_width,$new_height);
  }
  imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_width,$new_height,$old_width,$old_height);
  $smallname=$dir.$img_name.$small_ex.".".$img_ex;
  switch($img_ex) {
   case "jpg":
    imagejpeg($dst_img,$smallname,100);
    break;
   case "gif":
    imagegif($dst_img,$smallname);
    break;
   case "png":
    imagepng($dst_img,$smallname);
    break;
  }
 }
 return $smallname;
}

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.