php等比例缩放图片及剪切图片代码分享_php实例

php等比例缩放图片及剪切图片代码分享

/**
 * 图片缩放函数(可设置高度固定,宽度固定或者最大宽高,支持gif/jpg/png三种类型)
 * Author : Specs
 *
 * @param string $source_path 源图片
 * @param int $target_width 目标宽度
 * @param int $target_height 目标高度
 * @param string $fixed_orig 锁定宽高(可选参数 width、height或者空值)
 * @return string
 */
function myImageResize($source_path, $target_width = 200, $target_height = 200, $fixed_orig = ''){
  $source_info = getimagesize($source_path);
  $source_width = $source_info[0];
  $source_height = $source_info[1];
  $source_mime = $source_info['mime'];
  $ratio_orig = $source_width / $source_height;
  if ($fixed_orig == 'width'){
    //宽度固定
    $target_height = $target_width / $ratio_orig;
  }elseif ($fixed_orig == 'height'){
    //高度固定
    $target_width = $target_height * $ratio_orig;
  }else{
    //最大宽或最大高
    if ($target_width / $target_height > $ratio_orig){
      $target_width = $target_height * $ratio_orig;
    }else{
      $target_height = $target_width / $ratio_orig;
    }
  }
  switch ($source_mime){
    case 'image/gif':
      $source_image = imagecreatefromgif($source_path);
      break;

    case 'image/jpeg':
      $source_image = imagecreatefromjpeg($source_path);
      break;

    case 'image/png':
      $source_image = imagecreatefrompng($source_path);
      break;

    default:
      return false;
      break;
  }
  $target_image = imagecreatetruecolor($target_width, $target_height);
  imagecopyresampled($target_image, $source_image, 0, 0, 0, 0, $target_width, $target_height, $source_width, $source_height);
  //header('Content-type: image/jpeg');
  $imgArr = explode('.', $source_path);
  $target_path = $imgArr[0] . '_new.' . $imgArr[1];
  imagejpeg($target_image, $target_path, 100);
}

用法:

  1. myImageResize($filename, 200, 200); //最大宽高
  2. myImageResize($filename, 200, 200, 'width'); //宽度固定
  3. myImageResize($filename, 200, 200, 'height'); //高度固定

剪切图片为固定大小:

function imagecropper($source_path, $target_width, $target_height){
  $source_info = getimagesize($source_path);
  $source_width = $source_info[0];
  $source_height = $source_info[1];
  $source_mime = $source_info['mime'];
  $source_ratio = $source_height / $source_width;
  $target_ratio = $target_height / $target_width;

  // 源图过高
  if ($source_ratio > $target_ratio){
    $cropped_width = $source_width;
    $cropped_height = $source_width * $target_ratio;
    $source_x = 0;
    $source_y = ($source_height - $cropped_height) / 2;
  }elseif ($source_ratio < $target_ratio){ // 源图过宽
    $cropped_width = $source_height / $target_ratio;
    $cropped_height = $source_height;
    $source_x = ($source_width - $cropped_width) / 2;
    $source_y = 0;
  }else{ // 源图适中
    $cropped_width = $source_width;
    $cropped_height = $source_height;
    $source_x = 0;
    $source_y = 0;
  }

  switch ($source_mime){
    case 'image/gif':
      $source_image = imagecreatefromgif($source_path);
      break;

    case 'image/jpeg':
      $source_image = imagecreatefromjpeg($source_path);
      break;

    case 'image/png':
      $source_image = imagecreatefrompng($source_path);
      break;

    default:
      return false;
      break;
  }

  $target_image = imagecreatetruecolor($target_width, $target_height);
  $cropped_image = imagecreatetruecolor($cropped_width, $cropped_height);

  // 裁剪
  imagecopy($cropped_image, $source_image, 0, 0, $source_x, $source_y, $cropped_width, $cropped_height);
  // 缩放
  imagecopyresampled($target_image, $cropped_image, 0, 0, 0, 0, $target_width, $target_height, $cropped_width, $cropped_height);
  $dotpos = strrpos($source_path, '.');
  $imgName = substr($source_path, 0, $dotpos);
  $suffix = substr($source_path, $dotpos);
  $imgNew = $imgName . '_small' . $suffix;
  imagejpeg($target_image, $imgNew, 100);
  imagedestroy($source_image);
  imagedestroy($target_image);
  imagedestroy($cropped_image);
}

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索php等比剪切图片
java图片缩放剪切处理、touch.js图片缩放实例、revit 剪切 族实例、cad比例缩放、ps等比例缩放快捷键,以便于您获取更多的相关知识。

时间: 2024-09-13 20:11:08

php等比例缩放图片及剪切图片代码分享_php实例的相关文章

常用的php图片处理类(水印、等比缩放、固定高宽)分享_php实例

常用的php图片处理类(水印.等比缩放.固定高宽)分享 <?php //PHP 添加水印 & 比例缩略图 & 固定高度 & 固定宽度 类. class Image_process{ public $source; //原图 public $source_width; //原图宽度 public $source_height; //原图高度 public $source_type_id; public $orign_name; public $orign_dirname; //

php+flash+jQuery多图片上传源码分享_php实例

flash+php多图片上传的源码,测试成功,一个经典的上传源码,为什么要用flash作为上传的组件呢,其实这里不仅仅是flash,另加了jquery的技术,这样做的目的是为了更好更方便的管理图片,使用过QQ空间进行上传图片的童鞋都知道,QQ空间的上传体验度很好,而且管理我们上传的图片非常的方便,使用的技术基本上就是flash与jquery技术了. flash+jquery是作为前端图片上传展示的,还需要与php的结合才能将图片上传到指定的目标,这里的php一共有两个文件,一个upload.ph

用php实现百度网盘图片直链的代码分享_php实例

第一种代码:代码量较少通过正则表达式获取百度网盘的文件真实地址,来实现直链的效果 将下面的代码保存为downbd.php 复制代码 代码如下: <?php $canshu=$_SERVER["QUERY_STRING"]; if($canshu=="") { die("文件不存在"); } else { $wangzhi="http://pan.baidu.com/share/link?".$canshu; $file=

PHP图片等比缩放类SimpleImage使用方法和使用实例分享_php实例

使用方法示例:设定宽度,等比例缩放 复制代码 代码如下: <?php   include('SimpleImage.php');   $image = new SimpleImage();   $image->load('picture.jpg');   $image->resizeToWidth(250);   $image->save('picture2.jpg');?> 设定高度,等比例缩放 复制代码 代码如下: <?php   include('SimpleIm

php图片水印添加,压缩,剪切的封装类实现_php实例

php对图片文件的操作主要是利用GD库扩展.当我们频繁利用php对图片进行操作时,会自然封装很多函数,否则会写太多重复的代码.当有很多对图片的相关函数的时候,我们可以考虑将这些函数也整理一下,因而就有了封装成类的想法. 操作图片主要历经四个步骤:1.打开图片 2.操作图片 3.输出图片 4.销毁图片1,3,4三个步骤每次都要写,每次又都差不多.真正需要变通的只有操作图片的这一步骤了.操作图片又往往通过1或多个主要的GD函数来完成. <?php class Image { private $inf

jquery实现图片水平滚动效果代码分享_jquery

本文实例讲述了jquery实现图片水平滚动效果,分享给大家供大家参考.具体如下: 运行效果图:-------------------查看效果------------------- 小提示:浏览器中如果不能正常运行,可以尝试切换浏览模式. 为大家分享的jquery实现图片水平滚动效果代码如下 <HEAD> <META content="text/html; charset=gb2312" http-equiv="Content-Type"> &

PHP编写的图片验证码类文件分享_php实例

适用于自定义的验证码类! <?php /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ Class Image{ private $img; public $width = 85; pub

js支持键盘控制的左右切换立体式图片轮播效果代码分享_javascript技巧

本文实例讲述了js支持键盘控制的左右切换立体式图片轮播效果.分享给大家供大家参考.具体如下: 这是一款基于javascript实现支持键盘控制的左右切换立体式图片轮播效果,特别有立体感,最重要的一点是可以利用键盘进行控制. 特性介绍:      1.轻松的改变幻灯变的宽度.      2.轻易改变下一张展示图片的数量.      3.最后一张图片会循环回到第一张图片里.      4.嵌入了Fancy查看插件,在每张图片上都能查看详细图片信息. 运行效果图:                   

php使用base64加密解密图片示例分享_php实例

复制代码 代码如下: <?php //文件名:base64.php $data="/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2MBERISGBUYLxoaL2NCOEJjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY/