【问题标题】:Zend for picture uploads needing to resize imagesZend 用于需要调整图片大小的图片上传
【发布时间】:2013-05-20 11:12:53
【问题描述】:

所以,我使用 Zend 来处理我的图像上传。该脚本运行良好,但我想知道是否有办法调整正在上传的图像的大小,无论当前大小是多少。我看过 2 篇类似的帖子,但他们的代码完全不同,因此很难从他们的代码中翻译成我的。如果可能的话,我真的很想不必使用扩展,但如果必须的话,我会的。有什么想法吗?

if (isset($_POST['upload'])) {
require_once('scripter/lib.php');
//try {
$destination = 'C:\----';
$uploader = new Zend_File_Transfer_Adapter_Http();
$uploader->setDestination($destination);
$filename = $uploader->getFileName(NULL, FALSE);
$uploader->addValidator('Size', FALSE, '10000kB');
$uploader->addValidator('ImageSize', FALSE, array('minheight' => 100, 'minwidth' => 100));
//$pic = $filename;
if (!$uploader->isValid() || $errors) {
    $messages = $uploader->getMessages();
} else {
//$pic = $filename;
$no_spaces = str_replace(' ', '_', $filename, $renamed);
$uploader->addValidator('Extension', FALSE, 'gif, png, jpg');
$recognized = FALSE;
if ($uploader->isValid()) {
    $recognized = TRUE;
} else {
    $mime = $uploader->getMimeType();
    $acceptable = array('jpg' => 'image/jpeg',
                        'png' => 'image/png',
                        'gif' => 'image/gif');
    $key = array_search($mime, $acceptable);
    if (!$key) {
        $messages[] = 'Unrecognized image type';
    } else {
        $no_spaces = "$no_spaces.$key";
        $recognized = TRUE;
        $renamed = TRUE;
    }
}
$uploader->clearValidators();
if ($recognized) {
    $existing = scandir($destination);
    if (in_array($no_spaces, $existing)) {
        $dot = strrpos($no_spaces, '.');
        $base = substr($no_spaces, 0, $dot);
        $extension = substr($no_spaces, $dot);
        $i = 1;
        do {
            $no_spaces = $base . '_' . $i++ . $extension;
        } while (in_array($no_spaces, $existing));
        $renamed = TRUE;
    }
$uploader->addFilter('Rename', array('target' => $no_spaces));
$success = $uploader->receive();
if (!$success) {
$messages = $uploader->getMessages();
} else {
    //$pic = $no_spaces;
    $uploaded = "$filename uploaded successfully";
    $pic = $filename;
    if ($renamed) {
        $pic = "imgs/upld/" . $no_spaces;
        $uploaded .= " and renamed $no_spaces";
        //$pic = $no_spaces;
        //$pic = $uploader->getFileName(NULL, FALSE);

    } else {$pic = "imgs/upld/" . $filename;;}
    $messages[] = "$uploaded";
    //$pic = $no_spaces;

}

【问题讨论】:

  • 我也看到有人在另一个帖子上说要使用 Zend/Filter/ImageSize.php,但是没有名为 ImageSize.php 的文件,甚至在完整的最新版本中也没有。
  • 我猜我碰壁了,因为 polycaster zend 过滤器图像大小似乎坏了。

标签: zend-framework upload resize image


【解决方案1】:

Zend Framework 不附带处理图像的组件。

好消息! PHP 有几个组件非常擅长处理各种图像问题。

GD(伟大的 PHP 扩展之一)目前作为 PHP 的核心扩展提供,也许您会发现它很有用。

也许这会有所帮助:http://phpcodeforbeginner.blogspot.com/2013/04/resize-image-or-crop-image-using-gd.html

(不是真的想太刻薄;)

【讨论】:

  • 谢谢。 :) 我整晚都在寻找一种体面的方法来做到这一点。我肯定会试一试您的选择,尽管编辑我对您的示例所拥有的内容似乎有些困难。我发现的另一个有趣的帖子是 PHP Thumbnailer Class v2.0,虽然我没有尝试过。但是,是的,感谢您的帖子 RockyFord,我一定会好好阅读您的帖子建议,并在我进一步深入之前退后一步!
  • 有时很难记住 Zend Framework 只是 PHP 并且所有可用的 PHP 资源都可以工作。图像调整大小是一个常见问题,并且有很多解决方案。 phpclasses.org 通常是一个不错的起点。
猜你喜欢
  • 2012-08-01
  • 2011-08-25
  • 2014-08-26
  • 2011-05-28
  • 1970-01-01
  • 1970-01-01
  • 2013-12-19
相关资源
最近更新 更多