├── .gitignore ├── LICENSE.txt ├── README.md ├── composer.json ├── modman └── src ├── app ├── code │ └── community │ │ ├── FireGento │ │ └── PerfectWatermarks │ │ │ ├── Helper │ │ │ └── Data.php │ │ │ ├── Model │ │ │ ├── Product │ │ │ │ └── Image.php │ │ │ ├── Source │ │ │ │ └── Image │ │ │ │ │ └── Adapter.php │ │ │ └── System │ │ │ │ └── Config │ │ │ │ └── Adapter.php │ │ │ ├── Test │ │ │ ├── Config │ │ │ │ ├── Main.php │ │ │ │ └── System.php │ │ │ ├── Helper │ │ │ │ └── Data.php │ │ │ └── Model │ │ │ │ ├── Product │ │ │ │ └── Image.php │ │ │ │ └── System │ │ │ │ └── Config │ │ │ │ └── Adapter.php │ │ │ └── etc │ │ │ ├── config.xml │ │ │ └── system.xml │ │ └── Varien │ │ └── Image │ │ └── Adapter │ │ ├── Imagemagic.php │ │ └── icc_profiles │ │ ├── USWebUncoated.icc │ │ └── sRGB.icc └── etc │ └── modules │ └── FireGento_PerfectWatermarks.xml └── shell └── imagemagicktest.php /.gitignore: -------------------------------------------------------------------------------- 1 | .modgit/ 2 | app/code/community/Find/ 3 | app/code/community/Phoenix/ 4 | app/code/core/ 5 | app/design/adminhtml/default/default/ 6 | app/design/adminhtml/default/find/ 7 | app/design/frontend/base/ 8 | app/design/frontend/default/blank/ 9 | app/design/frontend/default/default/ 10 | app/design/frontend/default/iphone/ 11 | app/design/frontend/default/modern/ 12 | app/design/frontend/enterprise/default 13 | app/design/install/ 14 | app/etc/modules/Enterprise_* 15 | app/etc/modules/Find_Feed.xml 16 | app/etc/modules/Mage_All.xml 17 | app/etc/modules/Mage_Api.xml 18 | app/etc/modules/Mage_Authorizenet.xml 19 | app/etc/modules/Mage_Bundle.xml 20 | app/etc/modules/Mage_Centinel.xml 21 | app/etc/modules/Mage_Compiler.xml 22 | app/etc/modules/Mage_Connect.xml 23 | app/etc/modules/Mage_Downloadable.xml 24 | app/etc/modules/Mage_ImportExport.xml 25 | app/etc/modules/Mage_LoadTest.xml 26 | app/etc/modules/Mage_PageCache.xml 27 | app/etc/modules/Mage_Persistent.xml 28 | app/etc/modules/Mage_Weee.xml 29 | app/etc/modules/Mage_Widget.xml 30 | app/etc/modules/Mage_XmlConnect.xml 31 | app/etc/modules/Phoenix_Moneybookers.xml 32 | app/etc/config.xml 33 | app/etc/enterprise.xml 34 | app/etc/local.xml.additional 35 | app/etc/local.xml.template 36 | app/.htaccess 37 | app/locale/ 38 | app/Mage.php 39 | cron.php 40 | cron.sh 41 | downloader/ 42 | errors/ 43 | favicon.ico 44 | get.php 45 | includes/ 46 | index.php 47 | index.php.sample 48 | install.php 49 | js/blank.html 50 | js/calendar/ 51 | js/enterprise/ 52 | js/extjs/ 53 | js/firebug/ 54 | js/flash/ 55 | js/index.php 56 | js/jscolor/ 57 | js/lib/ 58 | js/mage/ 59 | js/prototype/ 60 | js/scriptaculous/ 61 | js/spacer.gif 62 | js/tiny_mce/ 63 | js/varien/ 64 | lib/3Dsecure/ 65 | lib/Apache/ 66 | lib/flex/ 67 | lib/googlecheckout/ 68 | lib/.htaccess 69 | lib/LinLibertineFont/ 70 | lib/Mage/ 71 | lib/PEAR/ 72 | lib/phpseclib/ 73 | lib/Varien/ 74 | lib/Zend/ 75 | LICENSE_AFL.txt 76 | LICENSE.html 77 | LICENSE.txt 78 | LICENSE_EE* 79 | mage 80 | media/customer/ 81 | media/downloadable/ 82 | media/.htaccess 83 | media/import/ 84 | media/xmlconnect/ 85 | nbproject/ 86 | pear 87 | pear/ 88 | php.ini.sample 89 | pkginfo/ 90 | RELEASE_NOTES.txt 91 | shell/abstract.php 92 | shell/compiler.php 93 | shell/indexer.php 94 | shell/log.php 95 | skin/adminhtml/default/default/ 96 | skin/adminhtml/default/enterprise 97 | skin/frontend/base/ 98 | skin/frontend/default/blank/ 99 | skin/frontend/default/blue/ 100 | skin/frontend/default/default/ 101 | skin/frontend/default/french/ 102 | skin/frontend/default/german/ 103 | skin/frontend/default/iphone/ 104 | skin/frontend/default/modern/ 105 | skin/frontend/enterprise 106 | skin/install/ 107 | var/ 108 | .idea/* 109 | data 110 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Open Software License ("OSL") v. 3.0 2 | This Open Software License (the "License") applies to any original work of authorship (the "Original Work") whose owner (the "Licensor") has placed the following licensing notice adjacent to the copyright notice for the Original Work: 3 | Licensed under the Open Software License version 3.0 4 | 1. Grant of Copyright License. Licensor grants You a worldwide, royalty-free, non-exclusive, sublicensable license, for the duration of the copyright, to do the following: 5 | 1. to reproduce the Original Work in copies, either alone or as part of a collective work; 6 | 2. to translate, adapt, alter, transform, modify, or arrange the Original Work, thereby creating derivative works ("Derivative Works") based upon the Original Work; 7 | 3. to distribute or communicate copies of the Original Work and Derivative Works to the public, with the proviso that copies of Original Work or Derivative Works that You distribute or communicate shall be licensed under this Open Software License; 8 | 4. to perform the Original Work publicly; and 9 | 5. to display the Original Work publicly. 10 | 2. Grant of Patent License. Licensor grants You a worldwide, royalty-free, non-exclusive, sublicensable license, under patent claims owned or controlled by the Licensor that are embodied in the Original Work as furnished by the Licensor, for the duration of the patents, to make, use, sell, offer for sale, have made, and import the Original Work and Derivative Works. 11 | 3. Grant of Source Code License. The term "Source Code" means the preferred form of the Original Work for making modifications to it and all available documentation describing how to modify the Original Work. Licensor agrees to provide a machine-readable copy of the Source Code of the Original Work along with each copy of the Original Work that Licensor distributes. Licensor reserves the right to satisfy this obligation by placing a machine-readable copy of the Source Code in an information repository reasonably calculated to permit inexpensive and convenient access by You for as long as Licensor continues to distribute the Original Work. 12 | 4. Exclusions From License Grant. Neither the names of Licensor, nor the names of any contributors to the Original Work, nor any of their trademarks or service marks, may be used to endorse or promote products derived from this Original Work without express prior permission of the Licensor. Except as expressly stated herein, nothing in this License grants any license to Licensor's trademarks, copyrights, patents, trade secrets or any other intellectual property. No patent license is granted to make, use, sell, offer for sale, have made, or import embodiments of any patent claims other than the licensed claims defined in Section 2. No license is granted to the trademarks of Licensor even if such marks are included in the Original Work. Nothing in this License shall be interpreted to prohibit Licensor from licensing under terms different from this License any Original Work that Licensor otherwise would have a right to license. 13 | 5. External Deployment. The term "External Deployment" means the use, distribution, or communication of the Original Work or Derivative Works in any way such that the Original Work or Derivative Works may be used by anyone other than You, whether those works are distributed or communicated to those persons or made available as an application intended for use over a network. As an express condition for the grants of license hereunder, You must treat any External Deployment by You of the Original Work or a Derivative Work as a distribution under section 1(c). 14 | 6. Attribution Rights. You must retain, in the Source Code of any Derivative Works that You create, all copyright, patent, or trademark notices from the Source Code of the Original Work, as well as any notices of licensing and any descriptive text identified therein as an "Attribution Notice." You must cause the Source Code for any Derivative Works that You create to carry a prominent Attribution Notice reasonably calculated to inform recipients that You have modified the Original Work. 15 | 7. Warranty of Provenance and Disclaimer of Warranty. Licensor warrants that the copyright in and to the Original Work and the patent rights granted herein by Licensor are owned by the Licensor or are sublicensed to You under the terms of this License with the permission of the contributor(s) of those copyrights and patent rights. Except as expressly stated in the immediately preceding sentence, the Original Work is provided under this License on an "AS IS" BASIS and WITHOUT WARRANTY, either express or implied, including, without limitation, the warranties of non-infringement, merchantability or fitness for a particular purpose. THE ENTIRE RISK AS TO THE QUALITY OF THE ORIGINAL WORK IS WITH YOU. This DISCLAIMER OF WARRANTY constitutes an essential part of this License. No license to the Original Work is granted by this License except under this disclaimer. 16 | 8. Limitation of Liability. Under no circumstances and under no legal theory, whether in tort (including negligence), contract, or otherwise, shall the Licensor be liable to anyone for any indirect, special, incidental, or consequential damages of any character arising as a result of this License or the use of the Original Work including, without limitation, damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses. This limitation of liability shall not apply to the extent applicable law prohibits such limitation. 17 | 9. Acceptance and Termination. If, at any time, You expressly assented to this License, that assent indicates your clear and irrevocable acceptance of this License and all of its terms and conditions. If You distribute or communicate copies of the Original Work or a Derivative Work, You must make a reasonable effort under the circumstances to obtain the express assent of recipients to the terms of this License. This License conditions your rights to undertake the activities listed in Section 1, including your right to create Derivative Works based upon the Original Work, and doing so without honoring these terms and conditions is prohibited by copyright law and international treaty. Nothing in this License is intended to affect copyright exceptions and limitations (including 'fair use' or 'fair dealing'). This License shall terminate immediately and You may no longer exercise any of the rights granted to You by this License upon your failure to honor the conditions in Section 1(c). 18 | 10. Termination for Patent Action. This License shall terminate automatically and You may no longer exercise any of the rights granted to You by this License as of the date You commence an action, including a cross-claim or counterclaim, against Licensor or any licensee alleging that the Original Work infringes a patent. This termination provision shall not apply for an action alleging patent infringement by combinations of the Original Work with other software or hardware. 19 | 11. Jurisdiction, Venue and Governing Law. Any action or suit relating to this License may be brought only in the courts of a jurisdiction wherein the Licensor resides or in which Licensor conducts its primary business, and under the laws of that jurisdiction excluding its conflict-of-law provisions. The application of the United Nations Convention on Contracts for the International Sale of Goods is expressly excluded. Any use of the Original Work outside the scope of this License or after its termination shall be subject to the requirements and penalties of copyright or patent law in the appropriate jurisdiction. This section shall survive the termination of this License. 20 | 12. Attorneys' Fees. In any action to enforce the terms of this License or seeking damages relating thereto, the prevailing party shall be entitled to recover its costs and expenses, including, without limitation, reasonable attorneys' fees and costs incurred in connection with such action, including any appeal of such action. This section shall survive the termination of this License. 21 | 13. Miscellaneous. If any provision of this License is held to be unenforceable, such provision shall be reformed only to the extent necessary to make it enforceable. 22 | 14. Definition of "You" in This License. "You" throughout this License, whether in upper or lower case, means an individual or a legal entity exercising rights under, and complying with all of the terms of, this License. For legal entities, "You" includes any entity that controls, is controlled by, or is under common control with you. For purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. 23 | 15. Right to Use. You may use the Original Work in all ways not otherwise restricted or conditioned by this License or by law, and Licensor promises not to interfere with or be responsible for such uses by You. 24 | 16. Modification of This License. This License is Copyright © 2005 Lawrence Rosen. Permission is granted to copy, distribute, or communicate this License without modification. Nothing in this License permits You to modify this License as applied to the Original Work or to Derivative Works. However, You may modify the text of this License and copy, distribute or communicate your modified version (the "Modified License") and apply it to other original works of authorship subject to the following conditions: (i) You may not indicate in any way that your Modified License is the "Open Software License" or "OSL" and you may not use those names in the name of your Modified License; (ii) You must replace the notice specified in the first paragraph above with the notice "Licensed under " or with a notice of your own that is not confusingly similar to the notice in this License; and (iii) You may not claim that your original works are open source software unless your Modified License has been approved by Open Source Initiative (OSI) and You comply with its license review and certification process. 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Perfect_Watermarks 2 | ================== 3 | 4 | A replacement for Magento's GD2 image adapter with imagemagick. 5 | 6 | Requirements 7 | ------------ 8 | 9 | You will need Imagemagick installed and the corresponding php extension 10 | loaded. 11 | 12 | Contribution 13 | ------------ 14 | 15 | Perfect_Watermarks will be developed in respect to git flow branching model. 16 | 17 | * Please use develop for pull request. 18 | * master will allways be the current "stable" version 19 | 20 | Disclaimer 21 | ---------- 22 | 23 | Please note that in some case the use of imagemagick will produce 24 | very high load. There is kind of a bug in OpenMP Library. Please 25 | check that imagemagick isn't compiled with OpenMP extension. 26 | 27 | Please look here: 28 | 29 | - http://www.daniloaz.com/en/617/systems/high-cpu-load-when-converting-images-with-imagemagick/ 30 | - http://blog.dlcware.com/2010/12/imagemagick-openmp-and-really-bad-performance.html 31 | 32 | Magento will extensivly check for enough memory to work with the image. The 33 | extension will always assume that you will give enough memory to process all 34 | your pictures. XHProf proofed that without the memory checking, the image 35 | proccesing runs much faster. 36 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "magento-hackathon/perfect_watermarks", 3 | "type": "magento-module", 4 | "description": "Replacement for Magento's GD2 image adapter with imagemagick", 5 | "license": "OSL-3.0", 6 | "authors": [ 7 | { 8 | "name": "Karl Spies" 9 | }], 10 | "homepage": "https://github.com/magento-hackathon/Perfect_Watermarks" 11 | } 12 | -------------------------------------------------------------------------------- /modman: -------------------------------------------------------------------------------- 1 | src/app/code/community/FireGento/PerfectWatermarks/ app/code/community/FireGento/PerfectWatermarks/ 2 | src/app/etc/modules/FireGento_PerfectWatermarks.xml app/etc/modules/FireGento_PerfectWatermarks.xml 3 | src/app/code/community/Varien/Image/Adapter/Imagemagic.php app/code/community/Varien/Image/Adapter/Imagemagic.php 4 | src/shell/imagemagicktest.php shell/imagemagicktest.php 5 | -------------------------------------------------------------------------------- /src/app/code/community/FireGento/PerfectWatermarks/Helper/Data.php: -------------------------------------------------------------------------------- 1 | _disableMemoryCheck; 14 | } 15 | 16 | /** 17 | * @param $disableMemoryCheck 18 | */ 19 | public function setDisableMemoryCheck($disableMemoryCheck) 20 | { 21 | $this->_disableMemoryCheck = $disableMemoryCheck; 22 | } 23 | 24 | /** 25 | * Overwriten to choose dynamically the image processor. 26 | * @return Varien_Image 27 | */ 28 | public function getImageProcessor() 29 | { 30 | if (!$this->_processor) { 31 | $this->_processor = new Varien_Image( 32 | $this->getBaseFile(), 33 | Mage::getStoreConfig('design/watermark_adapter/adapter') 34 | ); 35 | } 36 | $this->_processor->keepAspectRatio($this->_keepAspectRatio); 37 | $this->_processor->keepFrame($this->_keepFrame); 38 | $this->_processor->keepTransparency($this->_keepTransparency); 39 | $this->_processor->constrainOnly($this->_constrainOnly); 40 | $this->_processor->backgroundColor($this->_backgroundColor); 41 | $this->_processor->quality($this->_quality); 42 | return $this->_processor; 43 | } 44 | 45 | /** 46 | * @param null $file 47 | * @return bool 48 | */ 49 | protected function _checkMemory($file = null) 50 | { 51 | if ($this->getDisableMemoryCheck()) { 52 | return true; 53 | } else { 54 | return parent::_checkMemory($file = null); 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/app/code/community/FireGento/PerfectWatermarks/Model/Source/Image/Adapter.php: -------------------------------------------------------------------------------- 1 | Varien_Image_Adapter::ADAPTER_GD2, 14 | 'label' => Mage::helper('watermarks')->__('GD2 Adapter') 15 | ), 16 | array( 17 | 'value' => Varien_Image_Adapter::ADAPTER_IM, 18 | 'label' => Mage::helper('watermarks')->__('ImageMagick Adapter') 19 | ) 20 | ); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/app/code/community/FireGento/PerfectWatermarks/Model/System/Config/Adapter.php: -------------------------------------------------------------------------------- 1 | getImageAdapter()->checkDependencies(); 14 | } catch (Exception $e) { 15 | $this->setValue(Varien_Image_Adapter::ADAPTER_GD2); 16 | Mage::getSingleton('adminhtml/session')->addError( 17 | Mage::helper('watermarks') 18 | ->__('Selected library is not installed') 19 | ); 20 | } 21 | } 22 | 23 | /** 24 | * @param \Varien_Image_Adapter_Abstract $imageAdapter 25 | */ 26 | public function setImageAdapter($imageAdapter) 27 | { 28 | $this->_imageAdapter = $imageAdapter; 29 | } 30 | 31 | /** 32 | * @return \Varien_Image_Adapter_Abstract 33 | */ 34 | public function getImageAdapter() 35 | { 36 | if ($this->_imageAdapter === null) { 37 | $this->_imageAdapter = 38 | Mage::helper('watermarks')->getImageAdapter($this->getValue()); 39 | } 40 | return $this->_imageAdapter; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/app/code/community/FireGento/PerfectWatermarks/Test/Config/Main.php: -------------------------------------------------------------------------------- 1 | assertModelAlias( 8 | 'catalog/product_image', 9 | 'FireGento_PerfectWatermarks_Model_Product_Image' 10 | ); 11 | } 12 | 13 | public function testWatermarkModelConfigurationDefined() 14 | { 15 | $this->assertModelAlias( 16 | 'watermarks/source_image_adapter', 17 | 'FireGento_PerfectWatermarks_Model_Source_Image_Adapter' 18 | ); 19 | } 20 | 21 | public function testWatermarkHelperConfigurationDefined() 22 | { 23 | $this->assertHelperAlias( 24 | 'watermarks', 25 | 'FireGento_PerfectWatermarks_Helper_Data' 26 | ); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/app/code/community/FireGento/PerfectWatermarks/Test/Config/System.php: -------------------------------------------------------------------------------- 1 | assertDefaultConfigValue( 9 | 'design/watermark/image_adapter', 10 | Varien_Image_Adapter::ADAPTER_GD2 11 | ); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/app/code/community/FireGento/PerfectWatermarks/Test/Helper/Data.php: -------------------------------------------------------------------------------- 1 | _helper = Mage::helper('watermarks'); 13 | } 14 | 15 | public function testGetImageAdapter() 16 | { 17 | $this->assertInstanceOf( 18 | 'Varien_Image_Adapter_Gd2', 19 | $this->_helper->getImageAdapter(Varien_Image_Adapter::ADAPTER_GD2) 20 | ); 21 | $this->assertInstanceOf( 22 | 'Varien_Image_Adapter_Imagemagic', 23 | $this->_helper->getImageAdapter(Varien_Image_Adapter::ADAPTER_IM) 24 | ); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/app/code/community/FireGento/PerfectWatermarks/Test/Model/Product/Image.php: -------------------------------------------------------------------------------- 1 | _model = Mage::getModel('catalog/product_image'); 12 | } 13 | 14 | public function testReturnsCorrectImageProcessorClass() 15 | { 16 | /** @var $image Varien_Image */ 17 | $image = $this->_model->getImageProcessor(); 18 | $adapterClass = EcomDev_Utils_Reflection::invokeRestrictedMethod( 19 | $image, '_getAdapter' 20 | ); 21 | 22 | $this->assertInstanceOf('Varien_Image_Adapter_Abstract', 23 | $adapterClass); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/app/code/community/FireGento/PerfectWatermarks/Test/Model/System/Config/Adapter.php: -------------------------------------------------------------------------------- 1 | _model = Mage::getModel('watermarks/system_config_adapter'); 14 | } 15 | 16 | public function testConfigurationNotSavedIfModuleNotInstalled() 17 | { 18 | $imagemagickMock = $this->getMock( 19 | 'Varien_Image_Adapter_Imagemagic', 20 | array('checkDependencies') 21 | ); 22 | 23 | $imagemagickMock 24 | ->expects($this->any()) 25 | ->method('checkDependencies') 26 | ->will($this->throwException(new Exception())); 27 | 28 | $sessionMock = $this->getModelMock( 29 | 'adminhtml/session', 30 | array(), 31 | false, 32 | array(), 33 | '', 34 | false 35 | ); 36 | $sessionMock 37 | ->expects($this->once()) 38 | ->method('addError') 39 | ->withAnyParameters(); 40 | 41 | $this->replaceByMock('singleton', 'adminhtml/session', $sessionMock); 42 | 43 | $this->_model->setImageAdapter($imagemagickMock); 44 | $this->_model->setValue(Varien_Image_Adapter::ADAPTER_IM); 45 | 46 | EcomDev_Utils_Reflection::invokeRestrictedMethod( 47 | $this->_model, '_beforeSave' 48 | ); 49 | 50 | $this->assertEquals( 51 | Varien_Image_Adapter::ADAPTER_GD2, 52 | $this->_model->getValue() 53 | ); 54 | 55 | } 56 | 57 | public function testConfigurationSavedIfModuleNotInstalled() 58 | { 59 | $imagemagickMock = $this->getMock( 60 | 'Varien_Image_Adapter_Imagemagic', 61 | array('checkDependencies') 62 | ); 63 | 64 | $imagemagickMock 65 | ->expects($this->any()) 66 | ->method('checkDependencies') 67 | ->will($this->returnValue(true)); 68 | 69 | $this->_model->setImageAdapter($imagemagickMock); 70 | $this->_model->setValue(Varien_Image_Adapter::ADAPTER_GD2); 71 | 72 | EcomDev_Utils_Reflection::invokeRestrictedMethod( 73 | $this->_model, '_beforeSave' 74 | ); 75 | 76 | $this->assertEquals( 77 | Varien_Image_Adapter::ADAPTER_GD2, 78 | $this->_model->getValue() 79 | ); 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /src/app/code/community/FireGento/PerfectWatermarks/etc/config.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 0.3 13 | 14 | 15 | 16 | 17 | 18 | FireGento_PerfectWatermarks_Model 19 | 20 | 21 | 22 | FireGento_PerfectWatermarks_Model_Product_Image 23 | 24 | 25 | 26 | 27 | 28 | FireGento_PerfectWatermarks_Helper 29 | 30 | 31 | 32 | 33 | 34 | 35 | GD2 36 | 1 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /src/app/code/community/FireGento/PerfectWatermarks/etc/system.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 399 9 | 1 10 | 1 11 | 1 12 | 13 | 14 | 15 | select 16 | 10 17 | Please choose your image adapter 18 | watermarks/source_image_adapter 19 | watermarks/system_config_adapter 20 | 1 21 | 1 22 | 1 23 | 24 | 25 | 26 | text 27 | 20 28 | Performance may sometimes be negatively impacted by >1 threads. 29 | 1 30 | IMAGEMAGIC 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /src/app/code/community/Varien/Image/Adapter/Imagemagic.php: -------------------------------------------------------------------------------- 1 | _imageHandler === null) { 19 | $this->_imageHandler = new Imagick(); 20 | if ($threadLimit = Mage::getStoreConfig('design/watermark_adapter/thread_limit')) { 21 | $this->_imageHandler->setResourceLimit(6,max(1,min((int)$threadLimit,24))); // No constant available for threads 22 | } 23 | } 24 | return $this->_imageHandler; 25 | } 26 | 27 | /** 28 | * @param $fileName 29 | */ 30 | public function open($fileName) 31 | { 32 | Varien_Profiler::start(__METHOD__); 33 | $this->_fileName = $fileName; 34 | $this->getMimeType(); 35 | $this->_getFileAttributes(); 36 | $this->getImageMagick()->readimage($fileName); 37 | Varien_Profiler::stop(__METHOD__); 38 | } 39 | 40 | /** 41 | * Write file to file system. 42 | * 43 | * @param null $destination 44 | * @param null $newName 45 | * @throws Exception 46 | */ 47 | public function save($destination = null, $newName = null) 48 | { 49 | Varien_Profiler::start(__METHOD__); 50 | if (isset($destination) && isset($newName)) { 51 | $fileName = $destination . "/" . $newName; 52 | } elseif (isset($destination) && !isset($newName)) { 53 | $info = pathinfo($destination); 54 | $fileName = $destination; 55 | $destination = $info['dirname']; 56 | } elseif (!isset($destination) && isset($newName)) { 57 | $fileName = $this->_fileSrcPath . "/" . $newName; 58 | } else { 59 | $fileName = $this->_fileSrcPath . $this->_fileSrcName; 60 | } 61 | 62 | $destinationDir = (isset($destination)) ? 63 | $destination : $this->_fileSrcPath; 64 | 65 | if (!is_writable($destinationDir)) { 66 | try { 67 | $io = new Varien_Io_File(); 68 | $io->mkdir($destination); 69 | } catch (Exception $e) { 70 | Varien_Profiler::stop(__METHOD__); 71 | throw 72 | new Exception( 73 | "Unable to write into directory '{$destinationDir}'." 74 | ); 75 | } 76 | } 77 | //set compression quality 78 | $this->getImageMagick()->setImageCompressionQuality( 79 | $this->getQuality() 80 | ); 81 | //remove all underlying information 82 | $this->getImageMagick()->stripImage(); 83 | //write to file system 84 | $this->getImageMagick()->writeImage($fileName); 85 | //clear data and free resources 86 | $this->getImageMagick()->clear(); 87 | $this->getImageMagick()->destroy(); 88 | Varien_Profiler::stop(__METHOD__); 89 | } 90 | 91 | /** 92 | * Just display the image 93 | */ 94 | public function display() 95 | { 96 | header("Content-type: " . $this->getMimeType()); 97 | echo $this->getImageMagick(); 98 | } 99 | 100 | /** 101 | * @param null $frameWidth 102 | * @param null $frameHeight 103 | * @throws Exception 104 | */ 105 | public function resize($frameWidth = null, $frameHeight = null) 106 | { 107 | if (empty($frameWidth) && empty($frameHeight)) { 108 | throw new Exception('Invalid image dimensions.'); 109 | } 110 | 111 | Varien_Profiler::start(__METHOD__); 112 | $imagick = $this->getImageMagick(); 113 | 114 | // calculate lacking dimension 115 | $origWidth = $imagick->getImageWidth(); 116 | $origHeight = $imagick->getImageHeight(); 117 | if ($this->keepFrame() === TRUE) { 118 | if (null === $frameWidth) { 119 | $frameWidth = $frameHeight; 120 | } elseif (null === $frameHeight) { 121 | $frameHeight = $frameWidth; 122 | } 123 | } else { 124 | if (null === $frameWidth) { 125 | $frameWidth = round($frameHeight * ($origWidth / $origHeight)); 126 | } elseif (null === $frameHeight) { 127 | $frameHeight = round($frameWidth * ($origHeight / $origWidth)); 128 | } 129 | } 130 | 131 | if ($this->_keepAspectRatio && $this->_constrainOnly) { 132 | if (($frameWidth >= $origWidth) && ($frameHeight >= $origHeight)) { 133 | $frameWidth = $origWidth; 134 | $frameHeight = $origHeight; 135 | } 136 | } 137 | 138 | // Resize 139 | $imagick->setimageinterpolatemethod(imagick::INTERPOLATE_BICUBIC); 140 | $imagick->scaleimage($frameWidth, $frameHeight, true); 141 | 142 | // Fill desired canvas 143 | if ($this->keepFrame() === TRUE 144 | && $frameWidth != $origWidth 145 | && $frameHeight != $origHeight 146 | ) { 147 | $composite = new Imagick(); 148 | $color = $this->_backgroundColor; 149 | if ($color 150 | && is_array($color) 151 | && count($color) == 3 152 | ) { 153 | $bgColor = new ImagickPixel( 154 | 'rgb(' . implode(',', $color) . ')' 155 | ); 156 | } else { 157 | $bgColor = new ImagickPixel('white'); 158 | } 159 | $composite->newimage($frameWidth, $frameHeight, $bgColor); 160 | $composite->setimageformat($imagick->getimageformat()); 161 | 162 | if($imagick->getimagecolorspace() == Imagick::COLORSPACE_CMYK) { 163 | $profiles = $imagick->getimageprofiles('*', false); 164 | // we're only interested if ICC profile(s) exist 165 | $has_icc_profile = (array_search('icc', $profiles) !== false); 166 | // if it doesnt have a CMYK ICC profile, we add one 167 | if ($has_icc_profile === false) { 168 | $icc_cmyk = file_get_contents(__DIR__ . '/icc_profiles/USWebUncoated.icc'); 169 | $imagick->profileImage('icc', $icc_cmyk); 170 | unset($icc_cmyk); 171 | } 172 | // then we add an RGB profile 173 | $icc_rgb = file_get_contents(__DIR__ . '/icc_profiles/sRGB.icc'); 174 | $imagick->profileImage('icc', $icc_rgb); 175 | unset($icc_rgb); 176 | $imagick->setimagecolorspace(Imagick::COLORSPACE_SRGB); 177 | } 178 | 179 | $composite->setimagecolorspace($imagick->getimagecolorspace()); 180 | $dstX = floor(($frameWidth - $imagick->getimagewidth()) / 2); 181 | $dstY = floor(($frameHeight - $imagick->getimageheight()) / 2); 182 | $composite->compositeimage( 183 | $imagick, 184 | Imagick::COMPOSITE_OVER, 185 | $dstX, 186 | $dstY 187 | ); 188 | $this->_imageHandler = $composite; 189 | $imagick->clear(); 190 | $imagick->destroy(); 191 | } 192 | 193 | $this->refreshImageDimensions(); 194 | 195 | Varien_Profiler::stop(__METHOD__); 196 | } 197 | 198 | /** 199 | * @param $angle 200 | */ 201 | public function rotate($angle) 202 | { 203 | $this->getImageMagick()->rotateimage(new ImagickPixel(), $angle); 204 | 205 | $this->refreshImageDimensions(); 206 | } 207 | 208 | /** 209 | * @param int $top 210 | * @param int $left 211 | * @param int $right 212 | * @param int $bottom 213 | */ 214 | public function crop($top = 0, $left = 0, $right = 0, $bottom = 0) 215 | { 216 | if ($left == 0 && $top == 0 && $right == 0 && $bottom == 0) { 217 | return; 218 | } 219 | 220 | $newWidth = $this->_imageSrcWidth - $left - $right; 221 | $newHeight = $this->_imageSrcHeight - $top - $bottom; 222 | 223 | /* because drlrdsen said so! */ 224 | $this->getImageMagick()->cropImage( 225 | $newWidth, 226 | $newHeight, 227 | $left, 228 | $top 229 | ); 230 | 231 | $this->refreshImageDimensions(); 232 | } 233 | 234 | /** 235 | * @param $watermarkImage 236 | * @param int $positionX 237 | * @param int $positionY 238 | * @param int $watermarkImageOpacity 239 | * @param bool $repeat 240 | */ 241 | public function watermark( 242 | $watermarkImage, 243 | $positionX = 0, 244 | $positionY = 0, 245 | $watermarkImageOpacity = 30, 246 | $repeat = false) 247 | { 248 | Varien_Profiler::start(__METHOD__); 249 | 250 | /** @var $watermark Imagick */ 251 | $watermark = new Imagick($watermarkImage); 252 | 253 | //better method to blow up small images. 254 | $watermark->setimageinterpolatemethod( 255 | Imagick::INTERPOLATE_NEARESTNEIGHBOR 256 | ); 257 | 258 | if ($this->_watermarkImageOpacity == null) { 259 | $opc = $watermarkImageOpacity; 260 | } else { 261 | $opc = $this->getWatermarkImageOpacity(); 262 | } 263 | 264 | $watermark->evaluateImage( 265 | Imagick::EVALUATE_MULTIPLY, 266 | $opc, 267 | Imagick::CHANNEL_ALPHA 268 | ); 269 | 270 | // how big are the images? 271 | $iWidth = $this->getImageMagick()->getImageWidth(); 272 | $iHeight = $this->getImageMagick()->getImageHeight(); 273 | 274 | //resize watermark to configuration size 275 | if ($this->getWatermarkWidth() && 276 | $this->getWatermarkHeigth() && 277 | ($this->getWatermarkPosition() != self::POSITION_STRETCH) 278 | ) { 279 | $watermark->scaleImage( 280 | $this->getWatermarkWidth(), 281 | $this->getWatermarkHeigth() 282 | ); 283 | } 284 | 285 | // get watermark size 286 | $wWidth = $watermark->getImageWidth(); 287 | $wHeight = $watermark->getImageHeight(); 288 | 289 | //check if watermark is still bigger then image. 290 | if ($iHeight < $wHeight || $iWidth < $wWidth) { 291 | // resize the watermark 292 | $watermark->scaleImage($iWidth, $iHeight); 293 | // get new size 294 | $wWidth = $watermark->getImageWidth(); 295 | $wHeight = $watermark->getImageHeight(); 296 | } 297 | 298 | $x = 0; 299 | $y = 0; 300 | 301 | switch ($this->getWatermarkPosition()) { 302 | case self::POSITION_CENTER: 303 | $x = ($iWidth - $wWidth) / 2; 304 | $y = ($iHeight - $wHeight) / 2; 305 | break; 306 | case self::POSITION_STRETCH: 307 | $watermark->scaleimage($iWidth, $iHeight); 308 | break; 309 | case self::POSITION_TOP_RIGHT: 310 | $x = $iWidth - $wWidth; 311 | break; 312 | case self::POSITION_BOTTOM_LEFT: 313 | $y = $iHeight - $wHeight; 314 | break; 315 | case self::POSITION_BOTTOM_RIGHT: 316 | $x = $iWidth - $wWidth; 317 | $y = $iHeight - $wHeight; 318 | break; 319 | default: 320 | break; 321 | 322 | } 323 | 324 | $this->getImageMagick()->compositeImage( 325 | $watermark, 326 | Imagick::COMPOSITE_OVER, 327 | $x, 328 | $y 329 | ); 330 | $watermark->clear(); 331 | $watermark->destroy(); 332 | Varien_Profiler::stop(__METHOD__); 333 | } 334 | 335 | /** 336 | * @return bool 337 | * @throws Exception 338 | */ 339 | public function checkDependencies() 340 | { 341 | foreach ($this->_requiredExtensions as $value) { 342 | if (!extension_loaded($value)) { 343 | throw 344 | new Exception( 345 | "Required PHP extension '{$value}' was not loaded." 346 | ); 347 | } 348 | } 349 | return true; 350 | } 351 | 352 | protected function refreshImageDimensions() 353 | { 354 | $this->_imageSrcWidth = $this->_imageHandler->getImageWidth(); 355 | $this->_imageSrcHeight = $this->_imageHandler->getImageHeight(); 356 | $this->_imageHandler->setImagePage($this->_imageSrcWidth, $this->_imageSrcHeight, 0, 0); 357 | } 358 | 359 | public function __destruct() 360 | { 361 | @$this->getImageMagick()->clear(); 362 | @$this->getImageMagick()->destroy(); 363 | } 364 | 365 | /** 366 | * @return int 367 | */ 368 | public function getQuality() 369 | { 370 | if ($this->_quality == null) { 371 | $this->_quality = 80; 372 | } 373 | return $this->_quality; 374 | } 375 | 376 | /** 377 | * @return float 378 | */ 379 | public function getWatermarkImageOpacity() 380 | { 381 | if ($this->_watermarkImageOpacity == 0) { 382 | return $this->_watermarkImageOpacity = 0; 383 | } 384 | return $this->_watermarkImageOpacity / 100; 385 | } 386 | } 387 | -------------------------------------------------------------------------------- /src/app/code/community/Varien/Image/Adapter/icc_profiles/USWebUncoated.icc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/magento-hackathon/Perfect_Watermarks/83f60347e980210a378fdb9a281ca9c7186a2541/src/app/code/community/Varien/Image/Adapter/icc_profiles/USWebUncoated.icc -------------------------------------------------------------------------------- /src/app/code/community/Varien/Image/Adapter/icc_profiles/sRGB.icc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/magento-hackathon/Perfect_Watermarks/83f60347e980210a378fdb9a281ca9c7186a2541/src/app/code/community/Varien/Image/Adapter/icc_profiles/sRGB.icc -------------------------------------------------------------------------------- /src/app/etc/modules/FireGento_PerfectWatermarks.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | true 6 | community 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /src/shell/imagemagicktest.php: -------------------------------------------------------------------------------- 1 | getArg('d')) { 15 | echo $this->usageHelp(); 16 | return; 17 | } 18 | /** @var $iterator SplFileObject[] */ 19 | $iterator = new DirectoryIterator($this->getArg('d')); 20 | $start = microtime(true); 21 | $i = 0; 22 | $max = 0; 23 | do { 24 | foreach ($iterator as $file) { 25 | if (!$file->isDir()) { 26 | $image = new Varien_Image( 27 | $this->getArg('d') . DS . $file->getFilename(), 28 | Varien_Image_Adapter::ADAPTER_IM 29 | ); 30 | $image->keepFrame(true); 31 | $image->keepAspectRatio(true); 32 | $image->keepTransparency(true); 33 | $image->backgroundColor(array(255, 255, 255)); 34 | $image->resize(186, 500); 35 | $image->setWatermarkImageOpacity(30); 36 | $image->setWatermarkPosition( 37 | Varien_Image_Adapter_Abstract::POSITION_TOP_LEFT 38 | ); 39 | $image->setWatermarkHeigth(100); 40 | $image->setWatermarkWidth(100); 41 | $image->quality(80); 42 | $watermark = 43 | $this->getArg('d') . DS . 'watermark' . DS . 'watermark.png'; 44 | if (is_readable($watermark)) { 45 | $image->watermark($watermark); 46 | } 47 | $image->save( 48 | $this->getArg('d') . DS . 'result', $file->getFilename() 49 | ); 50 | } 51 | } 52 | } while ($i++ < $max); 53 | $endMem = memory_get_usage(true); 54 | $end = microtime(true); 55 | 56 | echo "Duration in seconds: " . (($end - $start)) . PHP_EOL; 57 | echo "Memory usage in MB: " . (($endMem / 1024) / 1024) . PHP_EOL; 58 | } 59 | 60 | /** 61 | * Retrieve Usage Help Message 62 | * 63 | */ 64 | public function usageHelp() 65 | { 66 | return <<run(); 79 | --------------------------------------------------------------------------------