php将图片保存为不同尺寸图片的图片类实例

本文实例讲述了php将图片保存为不同规格的图片类。分享给大家供大家参考。具体如下:

图片处理类.imagecls.php如下:

<?php
/**
  图片处理类
 */
class imagecls
{
  /**
   * 文件信息
   */
  var $file = array();
  /**
   * 保存目录
   */
  var $dir = '';
  /**
   * 错误代码
   */
  var $error_code = 0;
  /**
   * 文件上传最大KB
   */
  var $max_size = -1;
  function es_imagecls()
  {
  }
  private function checkSize($size)
  {
    return !($size > $this->max_size) || (-1 == $this->max_size);
  }
  /**
   * 处理上传文件
   * @param array $file 上传的文件
   * @param string $dir 保存的目录
   * @return bool
   */
  function init($file, $dir = 'temp')
  {
    if(!is_array($file) || empty($file) || !$this->isUploadFile($file['tmp_name']) || trim($file['name']) == '' || $file['size'] == 0)
    {
      $this->file = array();
      $this->error_code = -1;
      return false;
    }
    else
    {
      $file['size'] = intval($file['size']);
      $file['name'] = trim($file['name']);
      $file['thumb'] = '';
      $file['ext'] = $this->fileExt($file['name']);
      $file['name'] = htmlspecialchars($file['name'], ENT_QUOTES);
      $file['is_image'] = $this->isImageExt($file['ext']);
      $file['file_dir'] = $this->getTargetDir($dir);
      $file['prefix'] = md5(microtime(true)).rand(10,99);
      $file['target'] = "./public/".$file['file_dir'].'/'.$file['prefix'].'.jpg'; //相对
      $file['local_target'] = APP_ROOT_PATH."public/".$file['file_dir'].'/'.$file['prefix'].'.jpg'; //物理
      $this->file = &$file;
      $this->error_code = 0;
      return true;
    }
  }
  /**
   * 保存文件
   * @return bool
   */
  function save()
  {
    if(empty($this->file) || empty($this->file['tmp_name']))
      $this->error_code = -101;
    elseif(!$this->checkSize($this->file['size']))
      $this->error_code = -105;
    elseif(!$this->file['is_image'])
      $this->error_code = -102;
    elseif(!$this->saveFile($this->file['tmp_name'], $this->file['local_target']))
      $this->error_code = -103;
    elseif($this->file['is_image'] && (!$this->file['image_info'] = $this->getImageInfo($this->file['local_target'], true)))
    {
      $this->error_code = -104;
      @unlink($this->file['local_target']);
    }
    else
    {
      $this->error_code = 0;
      return true;
    }
    return false;
  }
  /**
   * 获取错误代码
   * @return number
   */
  function error()
  {
    return $this->error_code;
  }
  /**
   * 获取文件扩展名
   * @return string
   */
  function fileExt($file_name)
  {
    return addslashes(strtolower(substr(strrchr($file_name, '.'), 1, 10)));
  }
  /**
   * 根据扩展名判断文件是否为图像
   * @param string $ext 扩展名
   * @return bool
   */
  function isImageExt($ext)
  {
    static $img_ext = array('jpg', 'jpeg', 'png', 'bmp','gif','giff');
    return in_array($ext, $img_ext) ? 1 : 0;
  }
  /**
   * 获取图像信息
   * @param string $target 文件路径
   * @return mixed
   */
  function getImageInfo($target)
  {
    $ext = es_imagecls::fileExt($target);
    $is_image = es_imagecls::isImageExt($ext);
    if(!$is_image)
      return false;
    elseif(!is_readable($target))
      return false;
    elseif($image_info = @getimagesize($target))
    {
      list($width, $height, $type) = !empty($image_info) ? $image_info : array('', '', '');
      $size = $width * $height;
      if($is_image && !in_array($type, array(1,2,3,6,13)))
        return false;
      $image_info['type'] = strtolower(substr(image_type_to_extension($image_info[2]),1));
      return $image_info;
    }
    else
      return false;
  }
  /**
   * 获取是否充许上传文件
   * @param string $source 文件路径
   * @return bool
   */
  function isUploadFile($source)
  {
    return $source && ($source != 'none') && (is_uploaded_file($source) || is_uploaded_file(str_replace('\\\\', '\\', $source)));
  }
  /**
   * 获取保存的路径
   * @param string $dir 指定的保存目录
   * @return string
   */
  function getTargetDir($dir)
  {
    if (!is_dir(APP_ROOT_PATH."public/".$dir)) {
       @mkdir(APP_ROOT_PATH."public/".$dir);
       @chmod(APP_ROOT_PATH."public/".$dir, 0777);
    }
    return $dir;
  }
  /**
   * 保存文件
   * @param string $source 源文件路径
   * @param string $target 目录文件路径
   * @return bool
   */
  private function saveFile($source, $target)
  {
    if(!es_imagecls::isUploadFile($source))
      $succeed = false;
    elseif(@copy($source, $target))
      $succeed = true;
    elseif(function_exists('move_uploaded_file') && @move_uploaded_file($source, $target))
      $succeed = true;
    elseif (@is_readable($source) && (@$fp_s = fopen($source, 'rb')) && (@$fp_t = fopen($target, 'wb')))
    {
      while (!feof($fp_s))
      {
        $s = @fread($fp_s, 1024 * 512);
        @fwrite($fp_t, $s);
      }
      fclose($fp_s);
      fclose($fp_t);
      $succeed = true;
    }
    if($succeed)
    {
      $this->error_code = 0;
      @chmod($target, 0644);
      @unlink($source);
    }
    else
    {
      $this->error_code = 0;
    }
    return $succeed;
  }
  public function thumb($image,$maxWidth=200,$maxHeight=50,$gen = 0,$interlace=true,$filepath = '',$is_preview = true)
  {
    $info = es_imagecls::getImageInfo($image);
    if($info !== false)
    {
      $srcWidth = $info[0];
      $srcHeight = $info[1];
      $type = $info['type'];
      $interlace = $interlace? 1:0;
      unset($info);
      if($maxWidth > 0 && $maxHeight > 0)
        $scale = min($maxWidth/$srcWidth, $maxHeight/$srcHeight); // 计算缩放比例
      elseif($maxWidth == 0)
        $scale = $maxHeight/$srcHeight;
      elseif($maxHeight == 0)
        $scale = $maxWidth/$srcWidth;
      $paths = pathinfo($image);
      $paths['filename'] = trim(strtolower($paths['basename']),".".strtolower($paths['extension']));
      $basefilename = explode("_",$paths['filename']);
      $basefilename = $basefilename[0];
      if(empty($filepath))
      {
        if($is_preview)
        $thumbname = $paths['dirname'].'/'.$basefilename.'_'.$maxWidth.'x'.$maxHeight.'.jpg';
        else
        $thumbname = $paths['dirname'].'/'.$basefilename.'o_'.$maxWidth.'x'.$maxHeight.'.jpg';
      }
      else
        $thumbname = $filepath;
      $thumburl = str_replace(APP_ROOT_PATH,'./',$thumbname);
      if($scale >= 1)
      {
        // 超过原图大小不再缩略
        $width  = $srcWidth;
        $height = $srcHeight;
        if(!$is_preview)
        {
          //非预览模式写入原图
          file_put_contents($thumbname,file_get_contents($image));  //用原图写入
          return array('url'=>$thumburl,'path'=>$thumbname);
        }
      }
      else
      {
        // 缩略图尺寸
        $width = (int)($srcWidth*$scale);
        $height = (int)($srcHeight*$scale);
      }
      if($gen == 1)
      {
        $width = $maxWidth;
        $height = $maxHeight;
      }
      // 载入原图
      $createFun = 'imagecreatefrom'.($type=='jpg'?'jpeg':$type);
      if(!function_exists($createFun))
        $createFun = 'imagecreatefromjpeg';
      $srcImg = $createFun($image);
      //创建缩略图
      if($type!='gif' && function_exists('imagecreatetruecolor'))
        $thumbImg = imagecreatetruecolor($width, $height);
      else
        $thumbImg = imagecreate($width, $height);
      $x = 0;
      $y = 0;
      if($gen == 1 && $maxWidth > 0 && $maxHeight > 0)
      {
        $resize_ratio = $maxWidth/$maxHeight;
        $src_ratio = $srcWidth/$srcHeight;
        if($src_ratio >= $resize_ratio)
        {
          $x = ($srcWidth - ($resize_ratio * $srcHeight)) / 2;
          $width = ($height * $srcWidth) / $srcHeight;
        }
        else
        {
          $y = ($srcHeight - ( (1 / $resize_ratio) * $srcWidth)) / 2;
          $height = ($width * $srcHeight) / $srcWidth;
        }
      }
      // 复制图片
      if(function_exists("imagecopyresampled"))
        imagecopyresampled($thumbImg, $srcImg, 0, 0, $x, $y, $width, $height, $srcWidth,$srcHeight);
      else
        imagecopyresized($thumbImg, $srcImg, 0, 0, $x, $y, $width, $height, $srcWidth,$srcHeight);
      if('gif'==$type || 'png'==$type) {
        $background_color = imagecolorallocate($thumbImg, 0,255,0); // 指派一个绿色
        imagecolortransparent($thumbImg,$background_color); // 设置为透明色,若注释掉该行则输出绿色的图
      }
      // 对jpeg图形设置隔行扫描
      if('jpg'==$type || 'jpeg'==$type)
        imageinterlace($thumbImg,$interlace);
      // 生成图片
      imagejpeg($thumbImg,$thumbname,100);
      imagedestroy($thumbImg);
      imagedestroy($srcImg);
      return array('url'=>$thumburl,'path'=>$thumbname);
     }
     return false;
  }
  public function make_thumb($srcImg,$srcWidth,$srcHeight,$type,$maxWidth=200,$maxHeight=50,$gen = 0)
  {
      $interlace = $interlace? 1:0;
      if($maxWidth > 0 && $maxHeight > 0)
        $scale = min($maxWidth/$srcWidth, $maxHeight/$srcHeight); // 计算缩放比例
      elseif($maxWidth == 0)
        $scale = $maxHeight/$srcHeight;
      elseif($maxHeight == 0)
        $scale = $maxWidth/$srcWidth;
      if($scale >= 1)
      {
        // 超过原图大小不再缩略
        $width  = $srcWidth;
        $height = $srcHeight;
      }
      else
      {
        // 缩略图尺寸
        $width = (int)($srcWidth*$scale);
        $height = (int)($srcHeight*$scale);
      }
      if($gen == 1)
      {
        $width = $maxWidth;
        $height = $maxHeight;
      }
      //创建缩略图
      if($type!='gif' && function_exists('imagecreatetruecolor'))
        $thumbImg = imagecreatetruecolor($width, $height);
      else
        $thumbImg = imagecreatetruecolor($width, $height);
      $x = 0;
      $y = 0;
      if($gen == 1 && $maxWidth > 0 && $maxHeight > 0)
      {
        $resize_ratio = $maxWidth/$maxHeight;
        $src_ratio = $srcWidth/$srcHeight;
        if($src_ratio >= $resize_ratio)
        {
          $x = ($srcWidth - ($resize_ratio * $srcHeight)) / 2;
          $width = ($height * $srcWidth) / $srcHeight;
        }
        else
        {
          $y = ($srcHeight - ( (1 / $resize_ratio) * $srcWidth)) / 2;
          $height = ($width * $srcHeight) / $srcWidth;
        }
      }
      // 复制图片
      if(function_exists("imagecopyresampled"))
        imagecopyresampled($thumbImg, $srcImg, 0, 0, $x, $y, $width, $height, $srcWidth,$srcHeight);
      else
        imagecopyresized($thumbImg, $srcImg, 0, 0, $x, $y, $width, $height, $srcWidth,$srcHeight);
      if('gif'==$type || 'png'==$type) {
        $background_color = imagecolorallocate($thumbImg, 255,255,255); // 指派一个绿色
        imagecolortransparent($thumbImg,$background_color); // 设置为透明色,若注释掉该行则输出绿色的图
      }
      // 对jpeg图形设置隔行扫描
      if('jpg'==$type || 'jpeg'==$type)
        imageinterlace($thumbImg,$interlace);
      return $thumbImg;
  }
  public function water($source,$water,$alpha=80,$position="0")
  {
    //检查文件是否存在
    if(!file_exists($source)||!file_exists($water))
      return false;
    //图片信息
    $sInfo = es_imagecls::getImageInfo($source);
    $wInfo = es_imagecls::getImageInfo($water);
    //如果图片小于水印图片,不生成图片
    if($sInfo["0"] < $wInfo["0"] || $sInfo['1'] < $wInfo['1'])
      return false;
    if(is_animated_gif($source))
    {
      require_once APP_ROOT_PATH."system/utils/gif_encoder.php";
      require_once APP_ROOT_PATH."system/utils/gif_reader.php";
      $gif = new GIFReader();
      $gif->load($source);
      foreach($gif->IMGS['frames'] as $k=>$img)
      {
        $im = imagecreatefromstring($gif->getgif($k));
        //为im加水印
        $sImage=$im;
        $wCreateFun="imagecreatefrom".$wInfo['type'];
        if(!function_exists($wCreateFun))
          $wCreateFun = 'imagecreatefromjpeg';
        $wImage=$wCreateFun($water);
        //设定图像的混色模式
        imagealphablending($wImage, true);
        switch (intval($position))
        {
          case 0: break;
          //左上
          case 1:
            $posY=0;
            $posX=0;
            //生成混合图像
            imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo[0],$wInfo[1],$alpha);
            break;
          //右上
          case 2:
            $posY=0;
            $posX=$sInfo[0]-$wInfo[0];
            //生成混合图像
            imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo[0],$wInfo[1],$alpha);
            break;
          //左下
          case 3:
            $posY=$sInfo[1]-$wInfo[1];
            $posX=0;
            //生成混合图像
            imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo[0],$wInfo[1],$alpha);
            break;
          //右下
          case 4:
            $posY=$sInfo[1]-$wInfo[1];
            $posX=$sInfo[0]-$wInfo[0];
            //生成混合图像
            imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo[0],$wInfo[1],$alpha);
            break;
          //居中
          case 5:
            $posY=$sInfo[1]/2-$wInfo[1]/2;
            $posX=$sInfo[0]/2-$wInfo[0]/2;
            //生成混合图像
            imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo[0],$wInfo[1],$alpha);
            break;
        }
        //end im加水印
        ob_start();
        imagegif($sImage);
        $content = ob_get_contents();
        ob_end_clean();
        $frames [ ] = $content;
        $framed [ ] = $img['frameDelay'];
      }
      $gif_maker = new GIFEncoder (
          $frames,
          $framed,
          0,
          2,
          0, 0, 0,
          "bin"  //bin为二进制  url为地址
       );
      $image_rs = $gif_maker->GetAnimation ( );
      //如果没有给出保存文件名,默认为原图像名
      @unlink($source);
      //保存图像
      file_put_contents($source,$image_rs);
      return true;
    }
    //建立图像
    $sCreateFun="imagecreatefrom".$sInfo['type'];
    if(!function_exists($sCreateFun))
      $sCreateFun = 'imagecreatefromjpeg';
    $sImage=$sCreateFun($source);
    $wCreateFun="imagecreatefrom".$wInfo['type'];
    if(!function_exists($wCreateFun))
      $wCreateFun = 'imagecreatefromjpeg';
    $wImage=$wCreateFun($water);
    //设定图像的混色模式
    imagealphablending($wImage, true);
    switch (intval($position))
    {
      case 0: break;
      //左上
      case 1:
        $posY=0;
        $posX=0;
        //生成混合图像
        imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo[0],$wInfo[1],$alpha);
        break;
      //右上
      case 2:
        $posY=0;
        $posX=$sInfo[0]-$wInfo[0];
        //生成混合图像
        imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo[0],$wInfo[1],$alpha);
        break;
      //左下
      case 3:
        $posY=$sInfo[1]-$wInfo[1];
        $posX=0;
        //生成混合图像
        imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo[0],$wInfo[1],$alpha);
        break;
      //右下
      case 4:
        $posY=$sInfo[1]-$wInfo[1];
        $posX=$sInfo[0]-$wInfo[0];
        //生成混合图像
        imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo[0],$wInfo[1],$alpha);
        break;
      //居中
      case 5:
        $posY=$sInfo[1]/2-$wInfo[1]/2;
        $posX=$sInfo[0]/2-$wInfo[0]/2;
        //生成混合图像
        imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo[0],$wInfo[1],$alpha);
        break;
    }
    //如果没有给出保存文件名,默认为原图像名
    @unlink($source);
    //保存图像
    imagejpeg($sImage,$source,100);
    imagedestroy($sImage);
  }
}
if(!function_exists('image_type_to_extension'))
{
  function image_type_to_extension($imagetype)
  {
    if(empty($imagetype))
      return false;
    switch($imagetype)
    {
      case IMAGETYPE_GIF  : return '.gif';
      case IMAGETYPE_JPEG  : return '.jpeg';
      case IMAGETYPE_PNG  : return '.png';
      case IMAGETYPE_SWF  : return '.swf';
      case IMAGETYPE_PSD  : return '.psd';
      case IMAGETYPE_BMP  : return '.bmp';
      case IMAGETYPE_TIFF_II : return '.tiff';
      case IMAGETYPE_TIFF_MM : return '.tiff';
      case IMAGETYPE_JPC  : return '.jpc';
      case IMAGETYPE_JP2  : return '.jp2';
      case IMAGETYPE_JPX  : return '.jpf';
      case IMAGETYPE_JB2  : return '.jb2';
      case IMAGETYPE_SWC  : return '.swc';
      case IMAGETYPE_IFF  : return '.aiff';
      case IMAGETYPE_WBMP  : return '.wbmp';
      case IMAGETYPE_XBM  : return '.xbm';
      default        : return false;
    }
  }
}
?>

2.get_spec_img()调用图片类,然后再用下面的方法保存不同规格的图片并返回图片连接

//获取相应规格的图片地址
//gen=0:保持比例缩放,不剪裁,如高为0,则保证宽度按比例缩放 gen=1:保证长宽,剪裁
function get_spec_image($img_path,$width=0,$height=0,$gen=0,$is_preview=true)
{
  if($width==0)
    $new_path = $img_path;
  else
  {
    $img_name = substr($img_path,0,-4);
    $img_ext = substr($img_path,-3);
    if($is_preview)
    $new_path = $img_name."_".$width."x".$height.".jpg";
    else
    $new_path = $img_name."o_".$width."x".$height.".jpg";
    if(!file_exists($new_path))
    {
      require_once "imagecls.php";
      $imagec = new imagecls();
      $thumb = $imagec->thumb($img_path,$width,$height,$gen,true,"",$is_preview);
      if(app_conf("PUBLIC_DOMAIN_ROOT")!='')
      {
        $paths = pathinfo($new_path);
        $path = str_replace("./","",$paths['dirname']);
        $filename = $paths['basename'];
        $pathwithoupublic = str_replace("public/","",$path);
            $file_data = @file_get_contents($path.$file);
            $img = @imagecreatefromstring($file_data);
            if($img!==false)
            {
              $save_path = "public/".$path;
              if(!is_dir($save_path))
              {
                @mk_dir($save_path);
              }
              @file_put_contents($save_path.$name,$file_data);
            }
      }
    }
  }
  return $new_path;
}

3.使用方法:

//im:将店铺图片保存为3种规格:小图:48x48,中图120x120,大图200x200
$small_url=get_spec_image($data['image'],48,48,0);
$<span id="result_box" class="short_text" lang="en"><span>middle_url</span></span>=get_spec_image($data['image'],120,120,0);
$big_url=get_spec_image($data['image'],200,200,0);

希望本文所述对大家的php程序设计有所帮助。

(0)

相关推荐

  • php实现上传图片生成缩略图示例

    功能很简单,代码中有注释,直接给大家上代码了 复制代码 代码如下: <?php/** * 上传图片生成缩略图 *  * 需要GD2库的支持 *  * 初始化时需要参数new thumbnails('需要缩略的图片的原始地址','缩略图的宽度','缩略图的高度','(可选参数)缩略图的保存路径'); * 如果最后一个参数不指定,那么缩略图就默认保存在原始图片的所在目录里的small文件夹里, * 如果不存在small文件夹,则会自动创建small文件夹 *  * 初始化之后需要调用方法produc

  • PHP中改变图片的尺寸大小的代码

    先介绍一个自己写的函数. 复制代码 代码如下: <?php $imgsrc = "http://www.nowamagic.net/images/3.jpg"; $width = 780; $height = 420; resizejpg($imgsrc,$imgdst,$width,$height); function resizejpg($imgsrc,$imgdst,$imgwidth,$imgheight) { //$imgsrc jpg格式图像路径 $imgdst jp

  • php实现按指定大小等比缩放生成上传图片缩略图的方法

    本文实例讲述了php实现按指定大小等比缩放生成上传图片缩略图的方法.分享给大家供大家参考.具体实现方法如下: 复制代码 代码如下: /**  * *  *等比缩放  * @param unknown_type $srcImage   源图片路径  * @param unknown_type $toFile     目标图片路径  * @param unknown_type $maxWidth   最大宽  * @param unknown_type $maxHeight  最大高  * @par

  • PHP图片自动裁切应付不同尺寸的显示

    如果做过那种门户站的朋友,肯定知道,一张图片可能会在不同的地方显示,大小不同,比例也不同, 如果只用一张图的话,那么肯定会变形,而且在显示小图的地方,链接 大图,又太浪费了.....用缩略图来处理,也不完美,因为每个地方出现的比例 大小可能都不一样 ,举个例子! 请看上图. 在这个地方,其实调去出来的是一个列表,但是 图片的大小是不一样的,有多大宽有的窄,,当遇到这样的情况的时候 你们怎么办呢,如果直接用原来的地址,肯定是会变形的,如果搞缩略图也不靠谱,这个调去是自动调去的,你根本不知道哪个图片

  • php修改上传图片尺寸的方法

    本文实例讲述了php修改上传图片尺寸的方法.分享给大家供大家参考.具体实现方法如下: <?php // This is the temporary file created by PHP $uploadedfile = $_FILES['uploadfile']['tmp_name']; // Create an Image from it so we can do the resize $src = imagecreatefromjpeg($uploadedfile); // Capture

  • php中使用getimagesize获取图片、flash等文件的尺寸信息实例

    如果你还想着通过解析swf文件头信息来获取flash文件的尺寸信息,那真的有点走远了.因为从PHP 4开始已经内置getimagesize函数来做这个事.其功能测定任何 GIF,JPG,PNG,SWF,SWC,PSD,TIFF,BMP,IFF,JP2,JPX,JB2,JPC,XBM 或 WBMP 图像文件的大小并返回图像的尺寸以及文件类型和一个可以用于普通 HTML 文件中 IMG 标记中的 height/width 文本字符串.而且从PHP 4.0.5起还支持参数是一个url.例如: 复制代码

  • php调整gif动画图片尺寸示例代码分享

    类的使用demo: 复制代码 代码如下: <?php require_once "roucheng.php";  $gr = new gifresizer; $gr->temp_dir = "keleyi"; $gr->resize("keleyi.gif","keleyi_resized.gif",500,500); ?> 类的源代码,保存为roucheng.php文件: 复制代码 代码如下: <

  • php实现高效获取图片尺寸的方法

    本文实例讲述了php实现高效获取图片尺寸的方法.分享给大家供大家参考.具体分析如下: php 获取图片尺寸的方法我们可以使用 getimagesize 获取图片尺寸,但是效率是很低的,首先需要获取整个的图片信息,然后再进行操作,下面的例子更科学算法更好,我们一起来看看吧. 方法可以用于快速获取图片尺寸信息,获取JPEG格式图片的尺寸信息,并且不需要下载读取整个图片,经测试这个函数不是对所有JPEG格式的图片都有效. 1.获取JPEG格式图片的尺寸信息,代码如下: 复制代码 代码如下: <?php

  • php将图片保存为不同尺寸图片的图片类实例

    本文实例讲述了php将图片保存为不同规格的图片类.分享给大家供大家参考.具体如下: 图片处理类.imagecls.php如下: <?php /** 图片处理类 */ class imagecls { /** * 文件信息 */ var $file = array(); /** * 保存目录 */ var $dir = ''; /** * 错误代码 */ var $error_code = 0; /** * 文件上传最大KB */ var $max_size = -1; function es_i

  • 详解Java实现批量压缩图片裁剪压缩多种尺寸缩略图一键批量上传图片

    10万+IT人都在关注的图片批量压缩上传方案(完整案例+代码) 背景需求:为了客户端访问图片资源时,加载图片更流畅,体验更好,通常不会直接用原图路径,需要根据不同的场景显示不同规格的缩略图,根据商品关键属性,能够获取到图片不同尺寸规格的图片路径,并且能根据不同缩略图直观看到商品的关键属性,需要写一个Java小工具把本地磁盘中的图片资源一键上传至分布式FastDFS文件服务器,并把图片信息存入本地数据库,PC端或者客户端查询商品时,就可以根据商品的业务属性.比如根据productId就能把商品相关

  • tensorflow将图片保存为tfrecord和tfrecord的读取方式

    tensorflow官方提供了3种方法来读取数据: 预加载数据(preloaded data):在TensorFlow图中定义常量或变量来保存所有的数据,适用于数据量不太大的情况.填充数据(feeding):通过Python产生数据,然后再把数据填充到后端. 从文件读取数据(reading from file):从文件中直接读取,然后通过队列管理器从文件中读取数据. 本文主要介绍第三种方法,通过tfrecord文件来保存和读取数据,对于前两种读取数据的方式也会进行一个简单的介绍. 项目下载git

  • php实现将base64格式图片保存在指定目录的方法

    本文实例讲述了php实现将base64格式图片保存在指定目录的方法.分享给大家供大家参考,具体如下: <?php header('Content-type:text/html;charset=utf-8'); $base64_image_content = $_POST['imgBase64']; //匹配出图片的格式 if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){ $ty

  • android中Glide实现加载图片保存至本地并加载回调监听

    Glide 加载图片使用到的两个记录 Glide 加载图片保存至本地指定路径 /** * Glide 加载图片保存到本地 * * imgUrl 图片地址 * imgName 图片名称 */ Glide.with(context).load(imgUrl).asBitmap().toBytes().into(new SimpleTarget<byte[]>() { @Override public void onResourceReady(byte[] bytes, GlideAnimation

  • Android实现长按图片保存至相册功能

    前言:前面写了一篇reactnative的学习笔记,说reactnative的Android框架中有很多福利,确实是的,也说到了我们app中的一个把图片保存到相册的功能,好吧,还是准备写一篇博客,就当笔记了- 先上几张app的图片: 一进app就是一个进度条加载图片(我待会也会说一下进度条view跟怎么监听图片加载过程): 图片加载完毕后: 长按图片进入相册可以看到我们保存的图片: 监听图片加载的loaddingview源码(不是很难,我就直接贴代码了): package com.leo.cam

  • Android 实现WebView点击图片查看大图列表及图片保存功能

    在日常开发过程中,有时候会遇到需要在app中嵌入网页,此时使用WebView实现效果,但在默认情况下是无法点击图片查看大图的,更无法保存图片.本文将就这一系列问题的实现进行说明. 图示: 项目的知识点: 加载网页后如何捕捉网页中的图片点击事件: 获取点击的图片资源后进行图片显示,获取整个页面所有的图片: 支持查看上下一张的图片以及对图片缩放显示: 对图片进行保存: 其他:图片缓存的处理(不用每次都重新加载已查看过的图片) 项目代码结构: 前期准备(添加权限.依赖和混淆设置): 添加权限: <us

  • Android长按imageview把图片保存到本地的实例代码

    工具类 之前用 AsyncTask 现在改用rxJava public class SaveImageUtils { public static void imageSave(final ImageView imageView, final int id) { Observable .create(new Observable.OnSubscribe<ImageView>() { @Override public void call(Subscriber<? super ImageVie

  • 沙盒路径获取以及图片保存到相簿的方法

    iphone沙盒(sandbox)中的几个目录获取方式: // 获取沙盒主目录路径 NSString *homeDir = NSHomeDirectory(); // 获取Documents目录路径 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *docDir = [paths objectAtIndex:0]; // 获取Cac

  • iOS 沙盒图片保存读取实例

    实例如下所示: //保存图片 -(void)saveImageDocuments:(UIImage *)image{ //拿到图片 UIImage *imagesave = image; NSString *path_sandox = NSHomeDirectory(); //设置一个图片的存储路径 NSString *imagePath = [path_sandox stringByAppendingString:@"/Documents/test.png"]; //把图片直接保存到

随机推荐