/** * $image : string */ function image_url($image, $type = null, $force_no_base = false, $use_temp = false, $new_ext = null) { if (!is_null($type)) { $images_types = sfConfig::get('app_images_types'); $suffix = $images_types[$type]['suffix']; } else { $suffix = ''; } list($image_name, $image_ext) = Images::getFileNameParts($image); $base_path = $force_no_base ? '' : sfConfig::get('app_static_url'); $base_path .= DIRECTORY_SEPARATOR . sfConfig::get('app_upload_dir') . DIRECTORY_SEPARATOR . ($use_temp ? sfConfig::get('app_images_temp_directory_name') : sfConfig::get('app_images_directory_name')) . DIRECTORY_SEPARATOR; return $base_path . $image_name . $suffix . (isset($new_ext) ? $new_ext : $image_ext); }
public function executeRotate() { $id = $this->getRequestParameter('id'); $referer = $this->getRequest()->getReferer(); $degrees = (int) $this->getRequestParameter('degrees'); if ($degrees !== 90 && $degrees !== -90) { $referer = $this->getRequest()->getReferer(); $this->setErrorAndRedirect('Bad rotation value', $referer); } $doc = Document::find('Image', $id); $this->document = $doc; if (!$doc) { $this->setErrorAndRedirect('Image not found', $referer); } // check if the user has the right for editing the image $this->filterAuthorizedPeople($id); $temp_dir = sfConfig::get('sf_upload_dir') . DIRECTORY_SEPARATOR . sfConfig::get('app_images_temp_directory_name') . DIRECTORY_SEPARATOR; $upload_dir = sfConfig::get('sf_upload_dir') . DIRECTORY_SEPARATOR . sfConfig::get('app_images_directory_name') . DIRECTORY_SEPARATOR; list($filename, $extension) = Images::getFileNameParts($doc->getFilename()); $unique_filename = c2cTools::generateUniqueName(); // because images on production get migrated after a while on a different server // we need to check if the file exists on disk before trying to rotate the image if (!file_exists($upload_dir . $filename . $extension)) { $this->setErrorAndRedirect('Image cannot be rotated anymore', $referer); } Images::rotateImage($upload_dir . $filename . $extension, $temp_dir . $unique_filename . $extension, $degrees); Images::generateThumbnails($unique_filename, $extension, $temp_dir); if (!Images::moveAll($unique_filename . $extension, $temp_dir, $upload_dir)) { $this->setErrorAndRedirect('Rotation failed', $referer); } // we don't create a new image document version, instead we directly // update the filename field and clear cache // We need to change it everytime it appears, since we could have several image versions // with same filename (if non-i18n data like categroies has been changed) try { $conn = sfDoctrine::Connection(); $conn->beginTransaction(); Doctrine_Query::create()->update('ImageArchive ia')->set('ia.filename', '?')->where('ia.id = ? AND ia.filename = ?', array($unique_filename . $extension, $id, $filename . $extension))->execute(); $conn->commit(); // Delete old files Images::removeAll($filename . $extension, $upload_dir); } catch (Exception $e) { $conn->rollback(); // delete rotated images Images::removeAll($unique_filename . $extension, $upload_dir); $this->setErrorAndRedirect('Rotation failed', $referer); } // clear cache of current doc $this->clearCache('images', $id); // clear views of the associated docs in every language (content+interface): $associated_docs = Association::findAllAssociatedDocs($id, array('id', 'module')); foreach ($associated_docs as $doc) { // clear their view cache $this->clearCache($doc['module'], $doc['id'], false, 'view'); } // redirect to view $this->setNoticeAndRedirect('Image rotated successfully', $referer); }