Skip to content

Commit

Permalink
Mage_Media - mini DOC block update (#766)
Browse files Browse the repository at this point in the history
- doc blocks added/fixed
- PSR2 fixes (whitespaces, linebreaks, ...)

Co-authored-by: Tymoteusz Motylewski <t.motylewski@gmail.com>
  • Loading branch information
sreichel and tmotyl authored May 18, 2020
1 parent ef058f5 commit 5f228bc
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 34 deletions.
46 changes: 32 additions & 14 deletions app/code/core/Mage/Media/Model/File/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,32 @@ protected function _getWriteAdapter()
return false;
}

public function load(Mage_Media_Model_Image $object, $file, $field=null)
/**
* @param Mage_Media_Model_Image $object
* @param mixed $file
* @param null $field
* @return $this
*/
public function load(Mage_Media_Model_Image $object, $file, $field = null)
{
// Do some implementation
return $this;
}

/**
* @param Mage_Media_Model_Image $object
* @return $this
*/
public function save(Mage_Media_Model_Image $object)
{
// Do some implementation
return $this;
}

/**
* @param Mage_Media_Model_Image $object
* @return $this
*/
public function delete(Mage_Media_Model_Image $object)
{
return $this;
Expand All @@ -79,12 +93,13 @@ public function delete(Mage_Media_Model_Image $object)
* Create image resource for operation from file
*
* @param Mage_Media_Model_Image $object
* @return $this
* @return bool|false|resource
* @throws Mage_Core_Exception
*/
public function getImage(Mage_Media_Model_Image $object)
{
$resource = false;
switch(strtolower($object->getExtension())) {
switch (strtolower($object->getExtension())) {
case 'jpg':
case 'jpeg':
$resource = imagecreatefromjpeg($object->getFilePath());
Expand All @@ -99,7 +114,7 @@ public function getImage(Mage_Media_Model_Image $object)
break;
}

if(!$resource) {
if (!$resource) {
Mage::throwException(Mage::helper('media')->__('The image does not exist or is invalid.'));
}

Expand All @@ -111,7 +126,7 @@ public function getImage(Mage_Media_Model_Image $object)
* Create tmp image resource for operations
*
* @param Mage_Media_Model_Image $object
* @return $this
* @return false|resource
*/
public function getTmpImage(Mage_Media_Model_Image $object)
{
Expand All @@ -133,7 +148,10 @@ public function resize(Mage_Media_Model_Image $object)
imagecopyresampled(
$tmpImage,
$sourceImage,
0, 0, 0, 0,
0,
0,
0,
0,
$object->getDestanationDimensions()->getWidth(),
$object->getDestanationDimensions()->getHeight(),
$object->getDimensions()->getWidth(),
Expand Down Expand Up @@ -161,9 +179,9 @@ public function watermark(Mage_Media_Model_Image $object)
* @param string|null $extension
* @return $this
*/
public function saveAs(Mage_Media_Model_Image $object, $extension=null)
public function saveAs(Mage_Media_Model_Image $object, $extension = null)
{
if(is_null($extension)) {
if (is_null($extension)) {
$extension = $object->getExtension();
}

Expand All @@ -181,7 +199,7 @@ public function saveAs(Mage_Media_Model_Image $object, $extension=null)
break;
}

if(!$result) {
if (!$result) {
Mage::throwException(Mage::helper('media')->__('An error occurred while creating the image.'));
}

Expand All @@ -197,7 +215,7 @@ public function saveAs(Mage_Media_Model_Image $object, $extension=null)
public function getDimensions(Mage_Media_Model_Image $object)
{
$info = @getimagesize($object->getFilePath());
if(!$info) {
if (!$info) {
Mage::throwException(Mage::helper('media')->__('The image does not exist or is invalid.'));
}

Expand All @@ -209,6 +227,7 @@ public function getDimensions(Mage_Media_Model_Image $object)
* Destroys resource object
*
* @param resource $resource
* @return Mage_Media_Model_File_Image
*/
public function destroyResource(&$resource)
{
Expand All @@ -219,16 +238,15 @@ public function destroyResource(&$resource)
/**
* Destroys resource object
*
* @param resource $resource
* @param Mage_Media_Model_Image $object
* @return bool
*/
public function hasSpecialImage(Mage_Media_Model_Image $object)
{
if(file_exists($object->getFilePath(true))) {
if (file_exists($object->getFilePath(true))) {
return true;
}

return false;
}


}
82 changes: 63 additions & 19 deletions app/code/core/Mage/Media/Model/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/


/**
* Media library Image model
*
* @category Mage
* @package Mage_Media
* @author Magento Core Team <core@magentocommerce.com>
*
* @method string getFileName()
* @method $this setFileName(string $value)
*/
class Mage_Media_Model_Image extends Mage_Core_Model_Abstract
{
Expand Down Expand Up @@ -72,7 +74,7 @@ protected function _construct()
* Set media image config instance
*
* @param Mage_Media_Model_Image_Config_Interface $config
* @return unknown
* @return Mage_Media_Model_Image
*/
public function setConfig(Mage_Media_Model_Image_Config_Interface $config)
{
Expand All @@ -90,18 +92,24 @@ public function getConfig()
return $this->_config;
}

/**
* @return resource
*/
public function getImage()
{
if(is_null($this->_image)) {
if (is_null($this->_image)) {
$this->_image = $this->_getResource()->getImage($this);
}

return $this->_image;
}

/**
* @return resource
*/
public function getTmpImage()
{
if(is_null($this->_image)) {
if (is_null($this->_image)) {
$this->_tmpImage = $this->_getResource()->getTmpImage($this);
}

Expand All @@ -115,7 +123,7 @@ public function getTmpImage()
*/
public function getDimensions()
{
if(!$this->getData('dimensions')) {
if (!$this->getData('dimensions')) {
$this->setData('dimensions', $this->_getResource()->getDimensions($this));
}
return $this->getData('dimensions');
Expand All @@ -128,21 +136,28 @@ public function getDimensions()
*/
public function getDestanationDimensions()
{
if(!$this->getData('destanation_dimensions')) {
if (!$this->getData('destanation_dimensions')) {
$this->setData('destanation_dimensions', clone $this->getDimensions());
}

return $this->getData('destanation_dimensions');
}

/**
* @return bool|string
*/
public function getExtension()
{
return substr($this->getFileName(), strrpos($this->getFileName(), '.')+1);
}

public function getFilePath($useParams=false)
/**
* @param bool $useParams
* @return string
*/
public function getFilePath($useParams = false)
{
if($useParams && sizeof($this->getParams())) {
if ($useParams && sizeof($this->getParams())) {
$changes = '.' . $this->getParamsSum();
} else {
$changes = '';
Expand All @@ -152,9 +167,13 @@ public function getFilePath($useParams=false)
. ( ( $useParams && $this->getParam('extension')) ? $this->getParam('extension') : $this->getExtension() );
}

public function getFileUrl($useParams=false)
/**
* @param bool $useParams
* @return string
*/
public function getFileUrl($useParams = false)
{
if($useParams && sizeof($this->getParams())) {
if ($useParams && sizeof($this->getParams())) {
$changes = '.' . $this->getParamsSum();
} else {
$changes = '';
Expand All @@ -164,14 +183,22 @@ public function getFileUrl($useParams=false)
. ( ( $useParams && $this->getParam('extension')) ? $this->getParam('extension') : $this->getExtension() );
}

/**
* @return bool|string
*/
public function getName()
{
return substr($this->getFileName(), 0, strrpos($this->getFileName(), '.'));
}

public function addParam($param, $value=null)
/**
* @param array|string $param
* @param string $value
* @return $this
*/
public function addParam($param, $value = null)
{
if(is_array($param)) {
if (is_array($param)) {
$this->_params = array_merge($this->_params, $param);
} else {
$this->_params[$param] = $value;
Expand All @@ -180,9 +207,14 @@ public function addParam($param, $value=null)
return $this;
}

public function setParam($param, $value=null)
/**
* @param array|string $param
* @param string $value
* @return $this
*/
public function setParam($param, $value = null)
{
if(is_array($param)) {
if (is_array($param)) {
$this->_params = $param;
} else {
$this->_params[$param] = $value;
Expand All @@ -191,20 +223,30 @@ public function setParam($param, $value=null)
return $this;
}

/**
* @param string $param
* @return string|null
*/
public function getParam($param)
{
if(isset($this->_params[$param])) {
if (isset($this->_params[$param])) {
return $this->_params[$param];
}

return null;
}

/**
* @return array
*/
public function getParams()
{
return $this->_params;
}

/**
* @return string
*/
public function getParamsSum()
{
return md5(serialize($this->_params));
Expand All @@ -219,7 +261,7 @@ public function getParamsSum()
* @param string $watermark
* @return string
*/
public function getSpecialLink($file, $size, $extension=null, $watermark=null)
public function getSpecialLink($file, $size, $extension = null, $watermark = null)
{
$this->_removeResources();
$this->setData(array());
Expand All @@ -230,9 +272,9 @@ public function getSpecialLink($file, $size, $extension=null, $watermark=null)
$this->addParam('watermark', $watermark);
$this->addParam('extension', $extension);

if(!$this->hasSpecialImage()) {
if (!$this->hasSpecialImage()) {
if (strpos($size, 'x')!==false) {
list($width, $height) = explode('x', $size);
list($width, $height) = explode('x', $size);
} else {
$width = $size;
$height = $this->getDimensions()->getHeight();
Expand Down Expand Up @@ -261,6 +303,9 @@ public function getSpecialLink($file, $size, $extension=null, $watermark=null)
return $this->getFileUrl(true);
}

/**
* @return bool
*/
public function hasSpecialImage()
{
return $this->_getResource()->hasSpecialImage($this);
Expand All @@ -278,5 +323,4 @@ protected function _removeResources()
$this->_tmpImage = null;
}
}

}
1 change: 0 additions & 1 deletion app/code/core/Mage/Media/Model/Image/Config/Interface.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,4 @@ public function getMediaUrl($file);
* @return string
*/
public function getMediaPath($file);

}

0 comments on commit 5f228bc

Please sign in to comment.