Zend用于需要调整图像大小的图片上传

时间:2013-05-20 11:12:53

标签: zend-framework upload resize image

所以,我正在使用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;

}

1 个答案:

答案 0 :(得分:1)

Zend Framework没有附带用于处理图像的组件。

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

GD(其中一个优秀的PHP扩展)目前作为PHP的核心扩展提供,也许你会发现它很有用。

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

(并不是真的太过于苛刻;)