自动在浏览器中裁剪图像 [英] Cropping an Image In The Browser Automatically

查看:60
本文介绍了自动在浏览器中裁剪图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过上传过程自动裁剪图像?有没有PHP功能呢?

How can I crop an image automatically through the upload process? Is there php function to do that?

我希望我的网页通过裁剪来显示原始图像的各个维度具有相同尺寸的图像。

I want my webpage to display the images with the same dimension from various dimension of the original images by cropping it.

或者任何想法?

推荐答案

将其称为 max_size.php

<?php header('Content-type: image/jpeg');
function resampleimage($maxsize, $sourcefile, $imgcomp=0){
$g_imgcomp=100-$imgcomp;
    if(file_exists($sourcefile)){
    $g_is=getimagesize($sourcefile);
        if($g_is[0] <= $maxsize && $g_is[1] <= $maxsize){
            $new_width=$g_is[0];
            $new_height=$g_is[1];
        } else {
            $w_adjust = ($maxsize / $g_is[0]);
            $h_adjust = ($maxsize / $g_is[1]);
      if($w_adjust <= $h_adjust){
          $new_width=($g_is[0]*$w_adjust);
          $new_height=($g_is[1]*$w_adjust);
      } else {
          $new_width=($g_is[0]*$h_adjust);
          $new_height=($g_is[1]*$h_adjust);
      }
    }
    $image_type = strtolower(strrchr($sourcefile, "."));

    switch($image_type) {
      case '.jpg':
         $img_src = imagecreatefromjpeg($sourcefile);
         break;
      case '.jpeg':
         $img_src = imagecreatefromjpeg($sourcefile);
         break;
      case '.png':
         $img_src = imagecreatefrompng($sourcefile);
         break;
      case '.gif':
         $img_src = imagecreatefromgif($sourcefile);
         break;
      default:
         echo("Error Invalid Image Type");
         die;
         break;
   }
  $img_dst=imagecreatetruecolor($new_width,$new_height);
  imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $new_width, $new_height, $g_is[0], $g_is[1]);
  imagejpeg($img_dst);
  imagedestroy($img_dst);
  return true;
  } else {
  return false;
  }
}
resampleimage($_GET['maxsize'], $_GET['source']);
?>

在您有图像的页面中

<img id="img" src="max_size.php?maxsize=152&source=[some image path]" />

这篇关于自动在浏览器中裁剪图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆