【问题标题】:jQuery resize image before savingjQuery在保存之前调整图像大小
【发布时间】:2011-09-01 23:47:39
【问题描述】:

我是 jQuery 新手,但我喜欢它!我有一个问题我还不能解决。

我正在使用http://www.zurb.com/playground/ajax_upload

我已经使用以下 upload.php 开始工作

    <?
$time= time();
$uploaddir = 'users/'; //<--  Changed this to my directory for storing images
$uploadfile = $uploaddir.$time.basename($_FILES['userfile']['name']); //<-- IMPORTANT

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
    echo $uploaddir.$time.$_FILES['userfile']['name']; // IMPORTANT
    #print_r($_FILES);
} else {
  // WARNING! DO NOT USE "FALSE" STRING AS A RESPONSE!
  // Otherwise onSubmit event will not be fired
  echo "error";
}
?>

我添加了时间变量以确保每个图像都是唯一的。我遇到的问题是我想即时调整和优化图像的大小,但我不知道该怎么做。

调整大小是我需要的最重要的功能 - 例如,我希望保存的图像的最大宽度为 300 像素,即使它最初是 1000 像素宽。我需要按比例调整大小(这是一个词吗?:))

任何帮助都会很棒。

问候 M

【问题讨论】:

    标签: php jquery image-processing image-manipulation


    【解决方案1】:

    要调整图像大小,您需要像 GD 这样的库

    执行此操作的标准函数是 GD 的 imagecopyresampled。

    example 中显示了一种调整大小并保持比例的方法:

    //> MAx
    $width = 200;
    $height = 200;
    
    // Get new dimensions
    list($width_orig, $height_orig) = getimagesize($filename);
    
    $ratio_orig = $width_orig/$height_orig;
    
    if ($width/$height > $ratio_orig) {
       $width = $height*$ratio_orig;
    } else {
       $height = $width/$ratio_orig;
    }
    

    【讨论】:

    • 谢谢你,我在服务器上安装了 GD 并且相当容易实现,再次感谢。
    【解决方案2】:

    PHP 中有两个主要的图像处理东西,GD 或 Imagemagick。两者都将能够做你需要的。您需要在 PHP 网络服务器上配置它们。

    【讨论】:

      猜你喜欢
      • 2012-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-28
      • 2013-07-26
      • 1970-01-01
      • 2013-05-09
      • 2011-07-31
      相关资源
      最近更新 更多