当前位置:首页 > PHP教程 > php图像处理 > 列表

PHP 图片处理

发布:smiling 来源: PHP粉丝网  添加日期:2022-03-27 09:38:35 浏览: 评论:0 

这篇文章主要介绍了PHP如何对图片处理,帮助大家更好的利用PHP处理图片,感兴趣的朋友可以了解下。

图片处理函数功能:缩放、剪切、相框、水印、锐化、旋转、翻转、透明度、反色处理并保存历史记录的思路:当有图片有改动时自动生成一张新图片

1、转Base64编码

  1. /** 
  2.  * 获取图片的Base64编码(不支持url) 
  3.  * @param $img_file 传入本地图片地址 
  4.  * @return string 
  5.  */ 
  6. function imgToBase64($img_file) { 
  7.  $img_base64 = ''; 
  8.  if (file_exists($img_file)) { 
  9.   $app_img_file = $img_file; // 图片路径 
  10.   $img_info = getimagesize($app_img_file); // 取得图片的大小,类型等 
  11.   //echo '<pre>' . print_r($img_info, true) . '</pre><br>'; 
  12.   list($width, $height, $type, $attr) = getimagesize($app_img_file); 
  13.   $fp = fopen($app_img_file, "r"); // 图片是否可读权限 
  14.   if ($fp) { 
  15.    $filesize = filesize($app_img_file); 
  16.    $content = fread($fp, $filesize); 
  17.    $file_content = chunk_split(base64_encode($content)); // base64编码 
  18.    switch ($type) {   //判读图片类型 
  19.     case 1: $img_type = "gif"; 
  20.      break; 
  21.     case 2: $img_type = "jpg"; 
  22.      break; 
  23.     case 3: $img_type = "png"; 
  24.      break; 
  25.    } 
  26.    $img_base64 = 'data:image/png;base64,' . $file_content;//合成图片的base64编码 
  27.   } 
  28.   fclose($fp); 
  29.  }else{ 
  30.   return $img_file; 
  31.  } 
  32.  return $img_base64; //返回图片的base64 
  33. } 

2、图片旋转

  1. /** 
  2.  * 图片旋转 
  3.  * @param $src 图片地址 
  4.  * @param $direction 1顺时针90 2 逆时针90 
  5.  * @return string 
  6.  */ 
  7. function imgturn($src, $direction = 1){ 
  8.  $ext = pathinfo($src)['extension']; 
  9.  switch ($ext) { 
  10.   case 'gif': 
  11.    $img = imagecreatefromgif($src); 
  12.    break; 
  13.   case 'jpg': 
  14.   case 'jpeg': 
  15.    $img = imagecreatefromjpeg($src); 
  16.    break; 
  17.   case 'png': 
  18.    $img = imagecreatefrompng($src); 
  19.    break; 
  20.   default: 
  21.    die('图片格式错误!'); 
  22.    break; 
  23.  } 
  24.  $width = imagesx($img); 
  25.  $height = imagesy($img); 
  26.  $img2 = imagecreatetruecolor($height, $width); 
  27.  //顺时针旋转90度 
  28.  if($direction == 1){ 
  29.   for ($x = 0; $x < $width; $x++) { 
  30.    for($y=0; $y<$height; $y++) { 
  31.     imagecopy($img2, $img, $height - 1 - $y, $x, $x, $y, 1, 1); 
  32.    } 
  33.   } 
  34.  }else if($direction == 2) { 
  35.   //逆时针旋转90度 
  36.   for ($x = 0; $x < $height; $x++) { 
  37.    for($y = 0; $y < $width; $y++) { 
  38.     imagecopy($img2, $img, $x, $y, $width - 1 - $y, $x, 1, 1); 
  39.    } 
  40.   } 
  41.  } 
  42.  switch ($ext) { 
  43.   case 'jpg': 
  44.   case "jpeg": 
  45.    imagejpeg($img2, $src, 100); 
  46.    break; 
  47.   case "gif": 
  48.    imagegif($img2, $src, 100); 
  49.    break; 
  50.   case "png": 
  51.    imagepng($img2, $src, 100); 
  52.    break; 
  53.   default: 
  54.    die('图片格式错误!'); 
  55.    break; 
  56.  } 
  57.  imagedestroy($img); 
  58.  imagedestroy($img2); 
  59. } 

3、图片压缩

  1. /** 
  2. * 图片压缩处理 
  3. * @param string $sFile 源图片路径 
  4. * @param int $iWidth 自定义图片宽度 
  5. * @param int $iHeight 自定义图片高度 
  6. * @return string 压缩后的图片路径 
  7. */ 
  8. function getThumb($sFile, $iWidth, $iHeight){ 
  9.  //图片公共路径 
  10.  $public_path = ''; 
  11.  //判断该图片是否存在 
  12.  if(!file_exists($public_path . $sFile)) return $sFile; 
  13.  list($width, $height, $type, $attr) = getimagesize($sFile); 
  14.  if($width < $height){ 
  15.   imgturn($sFile, 2); 
  16.  } 
  17.  //判断图片格式(图片文件后缀) 
  18.  $extend = explode("." , $sFile); 
  19.  $attach_fileext = strtolower($extend[count($extend) - 1]); 
  20.  if (!in_array($attach_fileext, array('jpg','png','jpeg'))){ 
  21.   return ''; 
  22.  } 
  23.  //压缩图片文件名称 
  24.  $sFileNameS = str_replace("." . $attach_fileext, "_" . $iWidth . '_' . $iHeight . '.' . $attach_fileext, $sFile); 
  25.  //判断是否已压缩图片,若是则返回压缩图片路径 
  26.  if(file_exists($public_path . $sFileNameS)){ 
  27.   return $sFileNameS; 
  28.  } 
  29.  //生成压缩图片,并存储到原图同路径下 
  30.  resizeImage($public_path . $sFile, $public_path . $sFileNameS, $iWidth, $iHeight); 
  31.  if(!file_exists($public_path . $sFileNameS)){ 
  32.   return $sFile; 
  33.  } 
  34.  return $sFileNameS; 
  35. } 

4、生成目标图片

  1. /** 
  2.  * 生成图片 
  3.  * @param string $im 源图片路径 
  4.  * @param string $dest 目标图片路径 
  5.  * @param int $maxwidth 生成图片宽 
  6.  * @param int $maxheight 生成图片高 
  7.  */ 
  8. function resizeImage($im, $dest, $maxwidth, $maxheight) { 
  9.  $img = getimagesize($im); 
  10.  switch ($img[2]) { 
  11.   case 1: 
  12.    $im = @imagecreatefromgif($im); 
  13.   break; 
  14.   case 2: 
  15.    $im = @imagecreatefromjpeg($im); 
  16.   break; 
  17.   case 3: 
  18.    $im = @imagecreatefrompng($im); 
  19.   break; 
  20.  } 
  21.  $pic_width = imagesx($im); 
  22.  $pic_height = imagesy($im); 
  23.  $resizewidth_tag = false; 
  24.  $resizeheight_tag = false; 
  25.  if (($maxwidth && $pic_width > $maxwidth) || ($maxheight && $pic_height > $maxheight)) { 
  26.   if ($maxwidth && $pic_width > $maxwidth) { 
  27.    $widthratio = $maxwidth / $pic_width; 
  28.    $resizewidth_tag = true; 
  29.   } 
  30.   if ($maxheight && $pic_height > $maxheight) { 
  31.    $heightratio = $maxheight / $pic_height; 
  32.    $resizeheight_tag = true; 
  33.   } 
  34.   if ($resizewidth_tag && $resizeheight_tag) { 
  35.    if ($widthratio < $heightratio){ 
  36.     $ratio = $widthratio; 
  37.    }else{ 
  38.     $ratio = $heightratio; 
  39.    } 
  40.   } 
  41.   if ($resizewidth_tag && !$resizeheight_tag){ 
  42.    $ratio = $widthratio; 
  43.   } 
  44.   if ($resizeheight_tag && !$resizewidth_tag){ 
  45.    $ratio = $heightratio; 
  46.   } 
  47.   $newwidth = $pic_width * $ratio; 
  48.   $newheight = $pic_height * $ratio; 
  49.   if (function_exists("imagecopyresampled")) { 
  50.    $newim = imagecreatetruecolor($newwidth, $newheight); 
  51.    imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height); 
  52.   } else { 
  53.    $newim = imagecreate($newwidth, $newheight); 
  54.    imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height); 
  55.   } 
  56.   imagejpeg($newim, $dest); 
  57.   imagedestroy($newim); 
  58.  } else { 
  59.   imagejpeg($im, $dest); 
  60.  } 
  61. }

Tags: PHP图片处理

分享到: