phpimagezend-frameworkuploadresize

Zend for picture uploads needing to resize images


So, I am using Zend to handle my image uploads. This script works well, but I was wondering if there is a way to resize the image that is being uploaded no matter what the current size is. I've seen 2 similar posts, but their code was entirely different, thus would be difficult to translate into mine from theirs. If possible, I would really like to not have to use extensions, but I will if I have to. Any ideas?

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;

}

Solution

  • Zend Framework does not ship with a component for handling images.

    Good News! PHP has several components that are really good at dealing with all kinds of image issues.

    GD (one of those great PHP extensions) is currently shipped as a core extension for PHP, perhaps you will find it useful.

    Maybe this will help: http://phpcodeforbeginner.blogspot.com/2013/04/resize-image-or-crop-image-using-gd.html

    (not really trying to be too snarky ;)