jQuery在保存之前调整图像大小 [英] jQuery resize image before saving

查看:149
本文介绍了jQuery在保存之前调整图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是jQuery的新手,但我非常喜欢它!香港专业教育学院有一个问题,我现在还无法解决。

I am new to jQuery but im loving it! ive have a problem i cant get round as of yet.

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

我使用以下upload.php进行工作

which i have got working using the following 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";
}
?>

我添加了时间变量以确保每张图片都是唯一的。我的问题是我想要动态调整大小和优化图像,我不知道如何做到这一点。

i have added the time variable to ensure each image is unique. The problem i have is i want to resize and optimise the image on the fly and i am not sure how to do this.

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

The resize is the most important featuer i require - for example i would like a max width of 300px for the image that is saved even if it was originally 1000px wide. I need to resize proportionaly ( is that a word? :) )

任何帮助都会很棒。

问候
M

推荐答案

要调整图片大小,你需要像GD一样的库

To resize images you need libs like GD

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

The standard function to do this is GD's imagecopyresampled.

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

In the example is shown one way to resize and keeping the proportion:

//> 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;
}

这篇关于jQuery在保存之前调整图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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