Below you will find my function for the uploadAction. This code works. I am able to get images. However, I want to integrate an automatic resize mechanism to the code. Can someone explain to me how I could force files I am uploading in this upload Action to be parsed through my resizing function.
public function uploadAction()
{
$request = $this->getRequest();
$error = null;
if ($request->isPost()) {
if ($request->getParam('submit')) {
// Create upload object
$upload = new Zend_File_Transfer();
// Add our validators
$upload->addValidator('Size', false, 102400);
$upload->addValidator('Extension', false, 'jpg,png,gif');
// Set our destination
$upload->setDestination(APPLICATION_PATH . '/../public/images/blog/');
if(!$upload->receive()) {
$messages = $upload->getMessages();
foreach ($messages as $type => $message) {
switch($type) {
case 'fileExtensionFalse':
$this->view->error = 'File must be an image (jpg, gif, png)';
break;
case 'fileUploadErrorNoFile':
$this->view->error = 'Please select a file to upload';
break;
default:
$this->view->error = implode("/n", $messages);
break;
}
}
}
print_r($messages); die();
}
}
}
RESIZING FUNCTION
public function fastimagecopyresampled (&$dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h, $quality = 4)
{
if (empty($src_image) || empty($dst_image)) { return false; }
if ($quality <= 1)
{
$temp = imagecreatetruecolor ($dst_w + 1, $dst_h + 1);
imagecopyresized ($temp, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w + 1, $dst_h + 1, $src_w, $src_h);
imagecopyresized ($dst_image, $temp, 0, 0, 0, 0, $dst_w, $dst_h, $dst_w, $dst_h);
imagedestroy ($temp);
}
elseif ($quality < 5 && (($dst_w * $quality) < $src_w || ($dst_h * $quality) < $src_h))
{
$tmp_w = $dst_w * $quality;
$tmp_h = $dst_h * $quality;
$temp = imagecreatetruecolor ($tmp_w + 1, $tmp_h + 1);
imagecopyresized ($temp, $src_image, 0, 0, $src_x, $src_y, $tmp_w + 1, $tmp_h + 1, $src_w, $src_h);
imagecopyresampled ($dst_image, $temp, $dst_x, $dst_y, 0, 0, $dst_w, $dst_h, $tmp_w, $tmp_h);
imagedestroy ($temp);
} else
{
imagecopyresampled ($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
}
return true;
}
I want to be able to change the size of the image and the quality as it is uploaded. What is the function or call to access the file I JUST uploaded or can I manipulate the file before it is saved? Like upload the file, pass it through my function, then have the ouput from that become my “saved” file?
Create your own filter.
http://framework.zend.com/manual/1.12/en/zend.filter.writing_filters.html
In the filter method you will receive the path to the uploaded file. Resize it over there and return the new path. Then you can assign this filter in the upload object:
Detailed explanation of Zend_File_Transfer filters here