/** * Scales the image according to the configuration settings * * @param \Aimeos\MW\Media\Image\Iface $mediaFile Media object * @param string $type Type of the image like "preview" or "files" */ protected function scaleImage(\Aimeos\MW\Media\Image\Iface $mediaFile, $type) { $config = $this->context->getConfig(); /** controller/common/media/standard/files/maxwidth * Maximum width of the uploaded images * * The uploaded image files are scaled down if their width exceeds the * configured width of pixels. If the image width in smaller than the * configured one, no scaling happens. In case of a value of null or if * no configuration for that option is available, the image width isn't * scaled at all. * * The width/height ratio of the image is always kept. * * @param integer|null Width in pixel or null for no scaling * @since 2016.01 * @category Developer * @category User */ /** controller/common/media/standard/preview/maxwidth * Maximum width of the preview images * * The preview image files are created with the configured width in * pixel. If the original image width in smaller than the one configured * for the preview image, the width of the original image is used. In * case of a value of null or if no configuration for that option is * available, the width of the preview image is the same as the width of * the original image. * * The width/height ratio of the preview image is always the same as for * the original image. * * @param integer|null Width in pixel or null for no scaling * @since 2016.01 * @category Developer * @category User */ $maxwidth = $config->get('controller/common/media/standard/' . $type . '/maxwidth', null); /** controller/common/media/standard/files/maxheight * Maximum height of the uploaded images * * The uploaded image files are scaled down if their height exceeds the * configured height of pixels. If the image height in smaller than the * configured one, no scaling happens. In case of a value of null or if * no configuration for that option is available, the image width isn't * scaled at all. * * The width/height ratio of the image is always kept. * * @param integer|null Height in pixel or null for no scaling * @since 2016.01 * @category Developer * @category User */ /** controller/common/media/standard/preview/maxheight * Maximum height of the preview images * * The preview image files are created with the configured width in * pixel. If the original image height in smaller than the one configured * for the preview image, the height of the original image is used. In * case of a value of null or if no configuration for that option is * available, the height of the preview image is the same as the height * of the original image. * * The width/height ratio of the preview image is always the same as for * the original image. * * @param integer|null Height in pixel or null for no scaling * @since 2016.01 * @category Developer * @category User */ $maxheight = $config->get('controller/common/media/standard/' . $type . '/maxheight', null); $mediaFile->scale($maxwidth, $maxheight); }
/** * Creates a scaled image and returns it's new file name. * * @param \Aimeos\MW\Media\Image\Iface $mediaFile Media object * @param string $type Type of the image like "preview" or "files" * @param string $domain Domain the image belongs to, e.g. "product", "attribute", etc. * @param string $src Path to original file * @param string $filename Name of the new file without file extension * @return string Relative path to the new file * @throws \Aimeos\Controller\ExtJS\Exception If the configuration is invalid or due to insufficient permissions */ protected function createImage(\Aimeos\MW\Media\Image\Iface $mediaFile, $type, $domain, $src, $filename) { $mimetype = $mediaFile->getMimetype(); $config = $this->getContext()->getConfig(); /** controller/extjs/media/standard/files/allowedtypes * A list of image mime types that are allowed for uploaded image files * * The list of allowed image types must be explicitly configured for the * uploaded image files. Trying to upload and store an image file not * available in the list of allowed mime types will result in an exception. * * @param array List of image mime types * @since 2014.03 * @category Developer * @category User */ /** controller/extjs/media/standard/preview/allowedtypes * A list of image mime types that are allowed for preview image files * * The list of allowed image types must be explicitly configured for the * preview image files. Trying to create a preview image whose mime type * is not available in the list of allowed mime types will result in an * exception. * * @param array List of image mime types * @since 2014.03 * @category Developer * @category User */ $default = array('image/jpeg', 'image/png', 'image/gif'); $allowed = $config->get('controller/extjs/media/standard/' . $type . '/allowedtypes', $default); if (in_array($mimetype, $allowed) === false) { if (($defaulttype = reset($allowed)) !== false) { $mimetype = $defaulttype; } else { throw new \Aimeos\Controller\ExtJS\Exception(sprintf('No allowed image types configured for "%1$s"', $type)); } } if (($mediadir = $config->get('controller/extjs/media/standard/upload/directory', null)) === null) { throw new \Aimeos\Controller\ExtJS\Exception('No media directory configured'); } $ds = DIRECTORY_SEPARATOR; $fileext = $this->getFileExtension($mimetype); $filepath = $mediadir . $ds . $type . $ds . $domain . $ds . $filename[0] . $ds . $filename[1]; $dest = $this->getAbsoluteDirectory($filepath) . $ds . $filename . $fileext; /** controller/extjs/media/standard/files/maxwidth * Maximum width of the uploaded images * * The uploaded image files are scaled down if their width exceeds the * configured width of pixels. If the image width in smaller than the * configured one, no scaling happens. In case of a value of null or if * no configuration for that option is available, the image width isn't * scaled at all. * * The width/height ratio of the image is always kept. * * @param integer|null Width in pixel or null for no scaling * @since 2014.03 * @category Developer * @category User */ /** controller/extjs/media/standard/preview/maxwidth * Maximum width of the preview images * * The preview image files are created with the configured width in * pixel. If the original image width in smaller than the one configured * for the preview image, the width of the original image is used. In * case of a value of null or if no configuration for that option is * available, the width of the preview image is the same as the width of * the original image. * * The width/height ratio of the preview image is always the same as for * the original image. * * @param integer|null Width in pixel or null for no scaling * @since 2014.03 * @category Developer * @category User */ $maxwidth = $config->get('controller/extjs/media/standard/' . $type . '/maxwidth', null); /** controller/extjs/media/standard/files/maxheight * Maximum height of the uploaded images * * The uploaded image files are scaled down if their height exceeds the * configured height of pixels. If the image height in smaller than the * configured one, no scaling happens. In case of a value of null or if * no configuration for that option is available, the image width isn't * scaled at all. * * The width/height ratio of the image is always kept. * * @param integer|null Height in pixel or null for no scaling * @since 2014.03 * @category Developer * @category User */ /** controller/extjs/media/standard/preview/maxheight * Maximum height of the preview images * * The preview image files are created with the configured width in * pixel. If the original image height in smaller than the one configured * for the preview image, the height of the original image is used. In * case of a value of null or if no configuration for that option is * available, the height of the preview image is the same as the height * of the original image. * * The width/height ratio of the preview image is always the same as for * the original image. * * @param integer|null Height in pixel or null for no scaling * @since 2014.03 * @category Developer * @category User */ $maxheight = $config->get('controller/extjs/media/standard/' . $type . '/maxheight', null); $mediaFile->scale($maxwidth, $maxheight); $mediaFile->save($dest, $mimetype); /** controller/extjs/media/standard/upload/fileperms * File permissions used when storing uploaded or created files * * The representation of the permissions is in octal notation (using 0-7) * with a leading zero. The first number after the leading zero are the * permissions for the web server creating the directory, the second is * for the primary group of the web server and the last number represents * the permissions for everyone else. * * You should use 0775 or 0755 for the permissions as the web server needs * to manage the files and they are publically available, so it's not * necessary to limit read access for everyone else. The group permissions * are important if you plan to upload files directly via FTP or by other * means because then the web server needs to be able to read and manage * those files. In this case use 0775 as permissions, otherwise you can * limit them to 0755. * * A more detailed description of the meaning of the Unix file permission * bits can be found in the Wikipedia article about * {@link https://en.wikipedia.org/wiki/File_system_permissions#Numeric_notation file system permissions} * * @param integer Octal Unix permission representation * @since 2014.03 * @category Developer * @category User */ $perms = $config->get('controller/extjs/media/standard/upload/fileperms', 0664); if (chmod($dest, $perms) === false) { $msg = sprintf('Changing file permissions for "%1$s" to "%2$o" failed', $dest, $perms); $this->getContext()->getLogger()->log($msg, \Aimeos\MW\Logger\Base::WARN); } return "{$mediadir}/{$type}/{$domain}/{$filename[0]}/{$filename[1]}/{$filename}{$fileext}"; }
/** * Stores a scaled image and returns it's new file name. * * @param \Aimeos\MW\Media\Image\Iface $mediaFile Media object * @param string $type Type of the image like "preview" or "files" * @param string $filename Name of the new file without file extension * @param string $fsname Name of the file system to store the files at * @return string Relative path to the new file * @throws \Aimeos\Controller\Common\Exception If an error occurs */ protected function storeImage(\Aimeos\MW\Media\Image\Iface $mediaFile, $type, $filename, $fsname) { $file = $mediaFile->getFilepath(); $tmpfile = $this->getTempFileName(); $mimetype = $this->getMimeType($mediaFile, $type); $this->scaleImage($mediaFile, $type); $mediaFile->save($tmpfile, $mimetype); $fileext = $this->getFileExtension($mimetype); $dest = "{$type}/{$filename[0]}/{$filename[1]}/{$filename}{$fileext}"; $this->context->getFilesystemManager()->get($fsname)->writef($dest, $tmpfile); unlink($tmpfile); unlink($file); return $dest; }