Apache Zeta Components Manual :: File Source for imagemagick.php

Source for file imagemagick.php

Documentation is available at imagemagick.php

  1. <?php
  2. /**
  3.  * This file contains the ezcImageImagemagickHandler class.
  4.  *
  5.  * Licensed to the Apache Software Foundation (ASF) under one
  6.  * or more contributor license agreements.  See the NOTICE file
  7.  * distributed with this work for additional information
  8.  * regarding copyright ownership.  The ASF licenses this file
  9.  * to you under the Apache License, Version 2.0 (the
  10.  * "License"); you may not use this file except in compliance
  11.  * with the License.  You may obtain a copy of the License at
  12.  * 
  13.  *   http://www.apache.org/licenses/LICENSE-2.0
  14.  * 
  15.  * Unless required by applicable law or agreed to in writing,
  16.  * software distributed under the License is distributed on an
  17.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  18.  * KIND, either express or implied.  See the License for the
  19.  * specific language governing permissions and limitations
  20.  * under the License.
  21.  *
  22.  * @package ImageConversion
  23.  * @version //autogentag//
  24.  * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  25.  * @filesource
  26.  */
  27.  
  28. /**
  29.  * ezcImageHandler implementation for ImageMagick.
  30.  *
  31.  * @see ezcImageConverter
  32.  * @see ezcImageHandler
  33.  *
  34.  * @package ImageConversion
  35.  * @version //autogentag//
  36.  */
  37.                                  implements ezcImageGeometryFilters,
  38.                                             ezcImageColorspaceFilters,
  39.                                             ezcImageEffectFilters,
  40.                                             ezcImageWatermarkFilters,
  41.                                             ezcImageThumbnailFilters
  42. {
  43.  
  44.     /**
  45.      * Scale filter.
  46.      * General scale filter. Scales the image to fit into a given box size,
  47.      * determined by a given width and height value, measured in pixel. This
  48.      * method maintains the aspect ratio of the given image. Depending on the
  49.      * given direction value, this method performs the following scales:
  50.      *
  51.      * - ezcImageGeometryFilters::SCALE_BOTH:
  52.      *      The image will be scaled to fit exactly into the given box
  53.      *      dimensions, no matter if it was smaller or larger as the box
  54.      *      before.
  55.      * - ezcImageGeometryFilters::SCALE_DOWN:
  56.      *      The image will be scaled to fit exactly into the given box
  57.      *      only if it was larger than the given box dimensions before. If it
  58.      *      is smaller, the image will not be scaled at all.
  59.      * - ezcImageGeometryFilters::SCALE_UP:
  60.      *      The image will be scaled to fit exactly into the given box
  61.      *      only if it was smaller than the given box dimensions before. If it
  62.      *      is larger, the image will not be scaled at all. ATTENTION:
  63.      *      In this case, the image does not necessarily fit into the given box
  64.      *      afterwards.
  65.      *
  66.      * ATTENTION: Using this filter method directly results in the filter being
  67.      * applied to the image which is internally marked as "active" (most
  68.      * commonly this is the last recently loaded one). It is highly recommended
  69.      * to apply filters through the {@link ezcImageImagemagickHandler::applyFilter()}
  70.      * method, which enables you to specify the image a filter is applied to.
  71.      *
  72.      * @param int $width     Scale to width
  73.      * @param int $height    Scale to height
  74.      * @param int $direction Scale to which direction.
  75.      * @return void 
  76.      *
  77.      * @throws ezcImageInvalidReferenceException
  78.      *          No loaded file could be found or an error destroyed a loaded reference.
  79.      * @throws ezcBaseValueException
  80.      *          If a submitted parameter was out of range or type.
  81.      */
  82.     public function scale$width$height$direction ezcImageGeometryFilters::SCALE_BOTH )
  83.     {
  84.         if !is_int$width || $width )
  85.         {
  86.             throw new ezcBaseValueException'width'$width'int > 0' );
  87.         }
  88.         if !is_int$height || $height )
  89.         {
  90.             throw new ezcBaseValueException'height'$height'int > 0' );
  91.         }
  92.         
  93.         $dirMod $this->getDirectionModifier$direction );
  94.         $this->addFilterOption(
  95.             $this->getActiveReference(),
  96.             '-resize',
  97.             $width.$dirMod.'x'.$height.$dirMod
  98.         );
  99.     }
  100.  
  101.     /**
  102.      * Scale after width filter.
  103.      * Scales the image to a give width, measured in pixel. Scales the height
  104.      * automatically while keeping the ratio. The direction dictates, if an
  105.      * image may only be scaled {@link self::SCALE_UP}{@link self::SCALE_DOWN}
  106.      * or if the scale may work in {@link self::SCALE_BOTH} directions.
  107.      *
  108.      * ATTENTION: Using this filter method directly results in the filter being
  109.      * applied to the image which is internally marked as "active" (most
  110.      * commonly this is the last recently loaded one). It is highly recommended
  111.      * to apply filters through the {@link ezcImageImagemagickHandler::applyFilter()}
  112.      * method, which enables you to specify the image a filter is applied to.
  113.      *
  114.      * @param int $width     Scale to width
  115.      * @param int $direction Scale to which direction
  116.      * @return void 
  117.      *
  118.      * @throws ezcImageInvalidReferenceException
  119.      *          No loaded file could be found or an error destroyed a loaded reference
  120.      * @throws ezcBaseValueException
  121.      *          If a submitted parameter was out of range or type.
  122.      */
  123.     public function scaleWidth$width$direction )
  124.     {
  125.         if !is_int$width || $width )
  126.         {
  127.             throw new ezcBaseValueException'width'$width'int > 0' );
  128.         }
  129.  
  130.         $dirMod $this->getDirectionModifier$direction );
  131.         $this->addFilterOption(
  132.             $this->getActiveReference(),
  133.             '-resize ',
  134.             $width.$dirMod
  135.         );
  136.     }
  137.  
  138.     /**
  139.      * Scale after height filter.
  140.      * Scales the image to a give height, measured in pixel. Scales the width
  141.      * automatically while keeping the ratio. The direction dictates, if an
  142.      * image may only be scaled {@link self::SCALE_UP}{@link self::SCALE_DOWN}
  143.      * or if the scale may work in {@link self::SCALE_BOTH} directions.
  144.      *
  145.      * ATTENTION: Using this filter method directly results in the filter being
  146.      * applied to the image which is internally marked as "active" (most
  147.      * commonly this is the last recently loaded one). It is highly recommended
  148.      * to apply filters through the {@link ezcImageImagemagickHandler::applyFilter()}
  149.      * method, which enables you to specify the image a filter is applied to.
  150.      *
  151.      * @param int $height    Scale to height
  152.      * @param int $direction Scale to which direction
  153.      * @return void 
  154.      *
  155.      * @throws ezcImageInvalidReferenceException
  156.      *          No loaded file could be found or an error destroyed a loaded reference
  157.      * @throws ezcBaseValueException
  158.      *          If a submitted parameter was out of range or type.
  159.      */
  160.     public function scaleHeight$height$direction )
  161.     {
  162.         if !is_int$height || $height )
  163.         {
  164.             throw new ezcBaseValueException'height'$height'int > 0' );
  165.         }
  166.         $dirMod $this->getDirectionModifier$direction );
  167.         $this->addFilterOption(
  168.             $this->getActiveReference(),
  169.             '-resize ',
  170.             'x'.$height.$dirMod
  171.         );
  172.     }
  173.  
  174.     /**
  175.      * Scale percent measures filter.
  176.      * Scale an image to a given percentage value size.
  177.      *
  178.      * ATTENTION: Using this filter method directly results in the filter being
  179.      * applied to the image which is internally marked as "active" (most
  180.      * commonly this is the last recently loaded one). It is highly recommended
  181.      * to apply filters through the {@link ezcImageImagemagickHandler::applyFilter()}
  182.      * method, which enables you to specify the image a filter is applied to.
  183.      *
  184.      * @param int $width  Scale to width
  185.      * @param int $height Scale to height
  186.      * @return void 
  187.      *
  188.      * @throws ezcImageInvalidReferenceException
  189.      *          No loaded file could be found or an error destroyed a loaded reference
  190.      * @throws ezcBaseValueException
  191.      *          If a submitted parameter was out of range or type.
  192.      */
  193.     public function scalePercent$width$height )
  194.     {
  195.         if !is_int$height || $height )
  196.         {
  197.             throw new ezcBaseValueException'height'$height'int > 0' );
  198.         }
  199.         if !is_int$width || $width )
  200.         {
  201.             throw new ezcBaseValueException'width'$width'int > 0' );
  202.         }
  203.         $this->addFilterOption(
  204.             $this->getActiveReference(),
  205.             '-resize',
  206.             $width.'%x'.$height.'%'
  207.         );
  208.     }
  209.  
  210.     /**
  211.      * Scale exact filter.
  212.      * Scale the image to a fixed given pixel size, no matter to which
  213.      * direction.
  214.      *
  215.      * ATTENTION: Using this filter method directly results in the filter being
  216.      * applied to the image which is internally marked as "active" (most
  217.      * commonly this is the last recently loaded one). It is highly recommended
  218.      * to apply filters through the {@link ezcImageImagemagickHandler::applyFilter()}
  219.      * method, which enables you to specify the image a filter is applied to.
  220.      *
  221.      * @param int $width  Scale to width
  222.      * @param int $height Scale to height
  223.      * @return void 
  224.      *
  225.      * @throws ezcImageInvalidReferenceException
  226.      *          No loaded file could be found or an error destroyed a loaded reference.
  227.      * @throws ezcBaseValueException
  228.      *          If a submitted parameter was out of range or type.
  229.      */
  230.     public function scaleExact$width$height )
  231.     {
  232.         if !is_int$height || $height )
  233.         {
  234.             throw new ezcBaseValueException'height'$height'int > 0' );
  235.         }
  236.         if !is_int$width || $width )
  237.         {
  238.             throw new ezcBaseValueException'width'$width'int > 0' );
  239.         }
  240.         $this->addFilterOption(
  241.             $this->getActiveReference(),
  242.             '-resize',
  243.             $width.'!x'.$height.'!'
  244.         );
  245.     }
  246.  
  247.     /**
  248.      * Crop filter.
  249.      * Crop an image to a given size. This takes cartesian coordinates of a
  250.      * rect area to crop from the image. The cropped area will replace the old
  251.      * image resource (not the input image immediately, if you use the
  252.      * {@link ezcImageConverter}).  Coordinates are given as integer values and
  253.      * are measured from the top left corner.
  254.      *
  255.      * ATTENTION: Using this filter method directly results in the filter being
  256.      * applied to the image which is internally marked as "active" (most
  257.      * commonly this is the last recently loaded one). It is highly recommended
  258.      * to apply filters through the {@link ezcImageImagemagickHandler::applyFilter()}
  259.      * method, which enables you to specify the image a filter is applied to.
  260.      *
  261.      * @param int $x      X offset of the cropping area.
  262.      * @param int $y      Y offset of the cropping area.
  263.      * @param int $width  Width of cropping area.
  264.      * @param int $height Height of cropping area.
  265.      * @return void 
  266.      *
  267.      * @throws ezcImageInvalidReferenceException
  268.      *          No loaded file could be found or an error destroyed a loaded reference.
  269.      * @throws ezcBaseValueException
  270.      *          If a submitted parameter was out of range or type.
  271.      */
  272.     public function crop$x$y$width$height )
  273.     {
  274.         if !is_int$x ) )
  275.         {
  276.             throw new ezcBaseValueException'x'$x'int' );
  277.         }
  278.         if !is_int$y ) )
  279.         {
  280.             throw new ezcBaseValueException'y'$y'int' );
  281.         }
  282.         if !is_int$height ) )
  283.         {
  284.             throw new ezcBaseValueException'height'$height'int' );
  285.         }
  286.         if !is_int$width ) )
  287.         {
  288.             throw new ezcBaseValueException'width'$width'int' );
  289.         }
  290.  
  291.         $data getimagesize$this->getActiveResource() );
  292.         $x $x >= $x $data[0$x;
  293.         $y $y >= $y $data[1$y;
  294.  
  295.         $xStart $xStart min$x$x $width ) ) >= '+'.$xStart $xStart;
  296.         $yStart $yStart min$y$y $height ) ) >= '+'.$yStart $yStart;
  297.         $this->addFilterOption(
  298.             $this->getActiveReference(),
  299.             '-crop ',
  300.             abs$width ).'x'.abs$height ).$xStart.$yStart.'!'
  301.         );
  302.     }
  303.  
  304.     /**
  305.      * Colorspace filter.
  306.      * Transform the color space of the picture. The following color space are
  307.      * supported:
  308.      *
  309.      * - {@link self::COLORSPACE_GREY} - 255 grey colors
  310.      * - {@link self::COLORSPACE_SEPIA} - Sepia colors
  311.      * - {@link self::COLORSPACE_MONOCHROME} - 2 colors black and white
  312.      *
  313.      * ATTENTION: Using this filter method directly results in the filter being
  314.      * applied to the image which is internally marked as "active" (most
  315.      * commonly this is the last recently loaded one). It is highly recommended
  316.      * to apply filters through the {@link ezcImageImagemagickHandler::applyFilter()}
  317.      * method, which enables you to specify the image a filter is applied to.
  318.      *
  319.      * @param int $space Colorspace, one of self::COLORSPACE_* constants.
  320.      * @return void 
  321.      *
  322.      * @throws ezcImageInvalidReferenceException
  323.      *          No loaded file could be found or an error destroyed a loaded reference
  324.      * @throws ezcBaseValueException
  325.      *          If the parameter submitted as the colorspace was not within the
  326.      *          self::COLORSPACE_* constants.
  327.      */
  328.     public function colorspace$space )
  329.     {
  330.         switch $space )
  331.         {
  332.             case self::COLORSPACE_GREY:
  333.                 $this->addFilterOption(
  334.                     $this->getActiveReference(),
  335.                     '-colorspace',
  336.                     'GRAY'
  337.                 );
  338.                 $this->addFilterOption(
  339.                     $this->getActiveReference(),
  340.                     '-colors',
  341.                     '255'
  342.                 );
  343.                 break;
  344.             case self::COLORSPACE_MONOCHROME:
  345.                 $this->addFilterOption(
  346.                     $this->getActiveReference(),
  347.                     '-monochrome'
  348.                 );
  349.                 break;
  350.             case self::COLORSPACE_SEPIA:
  351.                 $this->addFilterOption(
  352.                     $this->getActiveReference(),
  353.                     '-sepia-tone',
  354.                     '80%'
  355.                 );
  356.                 break;
  357.             return;
  358.             default:
  359.                 throw new ezcBaseValueException'space'$space'self::COLORSPACE_GREY, self::COLORSPACE_SEPIA, self::COLORSPACE_MONOCHROME' );
  360.                 break;
  361.         }
  362.     }
  363.  
  364.     /**
  365.      * Noise filter.
  366.      * Apply a noise transformation to the image. Valid values are the following
  367.      * strings:
  368.      * - 'Uniform'
  369.      * - 'Gaussian'
  370.      * - 'Multiplicative'
  371.      * - 'Impulse'
  372.      * - 'Laplacian'
  373.      * - 'Poisson'
  374.      *
  375.      * ATTENTION: Using this filter method directly results in the filter being
  376.      * applied to the image which is internally marked as "active" (most
  377.      * commonly this is the last recently loaded one). It is highly recommended
  378.      * to apply filters through the {@link ezcImageImagemagickHandler::applyFilter()}
  379.      * method, which enables you to specify the image a filter is applied to.
  380.      *
  381.      * @param strings $value Noise value as described above.
  382.      * @return void 
  383.      *
  384.      * @throws ezcBaseValueException
  385.      *          If the noise value is out of range.
  386.      * @throws ezcImageInvalidReferenceException
  387.      *          No loaded file could be found or an error destroyed a loaded reference.
  388.      */
  389.     public function noise$value )
  390.     {
  391.         $value ucfirststrtolower$value ) );
  392.         $possibleValues array(
  393.            'Uniform',
  394.            'Gaussian',
  395.            'Multiplicative',
  396.            'Impulse',
  397.            'Laplacian',
  398.            'Poisson',
  399.         );
  400.         if !in_array$value$possibleValues ) )
  401.         {
  402.             throw new ezcBaseValueException'value'$value'Uniform, Gaussian, Multiplicative, Impulse, Laplacian, Poisson' );
  403.         }
  404.         $this->addFilterOption(
  405.             $this->getActiveReference(),
  406.             '+noise',
  407.             $value
  408.         );
  409.     }
  410.  
  411.     /**
  412.      * Swirl filter.
  413.      * Applies a swirl with the given intense to the image.
  414.      *
  415.      * ATTENTION: Using this filter method directly results in the filter being
  416.      * applied to the image which is internally marked as "active" (most
  417.      * commonly this is the last recently loaded one). It is highly recommended
  418.      * to apply filters through the {@link ezcImageImagemagickHandler::applyFilter()}
  419.      * method, which enables you to specify the image a filter is applied to.
  420.      *
  421.      * @param int $value Intense of swirl.
  422.      * @return void 
  423.      *
  424.      * @throws ezcImageInvalidReferenceException
  425.      *          No loaded file could be found or an error destroyed a loaded reference.
  426.      * @throws ezcBaseValueException
  427.      *          If the swirl value is out of range.
  428.      */
  429.     public function swirl$value )
  430.     {
  431.         if !is_int$value || $value )
  432.         {
  433.             throw new ezcBaseValueException'value'$value'int >= 0' );
  434.         }
  435.         $this->addFilterOption(
  436.             $this->getActiveReference(),
  437.             '-swirl',
  438.             $value
  439.         );
  440.     }
  441.  
  442.     /**
  443.      * Border filter.
  444.      * Adds a border to the image. The width is measured in pixel. The color is
  445.      * defined in an array of hex values:
  446.      *
  447.      * <code>
  448.      * array(
  449.      *      0 => <red value>,
  450.      *      1 => <green value>,
  451.      *      2 => <blue value>,
  452.      * );
  453.      * </code>
  454.      *
  455.      * ATTENTION: Using this filter method directly results in the filter being
  456.      * applied to the image which is internally marked as "active" (most
  457.      * commonly this is the last recently loaded one). It is highly recommended
  458.      * to apply filters through the {@link ezcImageImagemagickHandler::applyFilter()}
  459.      * method, which enables you to specify the image a filter is applied to.
  460.      *
  461.      * @param int $width        Width of the border.
  462.      * @param array(int) $color Color.
  463.      * @return void 
  464.      *
  465.      * @throws ezcImageInvalidReferenceException
  466.      *          No loaded file could be found or an error destroyed a loaded reference.
  467.      * @throws ezcBaseValueException
  468.      *          If a submitted parameter was out of range or type.
  469.      */
  470.     public function border$widtharray $color )
  471.     {
  472.         if !is_int$width ) )
  473.         {
  474.             throw new ezcBaseValueException'width'$width'int' );
  475.         }
  476.         $colorString $this->colorArrayToString$color );
  477.         $this->addFilterOption(
  478.             $this->getActiveReference(),
  479.             '-bordercolor',
  480.             $colorString
  481.         );
  482.         $this->addFilterOption(
  483.             $this->getActiveReference(),
  484.             '-border',
  485.             $width
  486.         );
  487.     }
  488.     
  489.     /**
  490.      * Watermark filter.
  491.      * Places a watermark on the image. The file to use as the watermark image
  492.      * is given as $image. The $posX, $posY and $size values are given in
  493.      * percent, related to the destination image. A $size value of 10 will make
  494.      * the watermark appear in 10% of the destination image size.
  495.      * $posX = $posY = 10 will make the watermark appear in the top left corner
  496.      * of the destination image, 10% of its size away from its borders. If
  497.      * $size is ommitted, the watermark image will not be resized.
  498.      *
  499.      * @param string $image  The image file to use as the watermark
  500.      * @param int $posX      X position in the destination image in percent.
  501.      * @param int $posY      Y position in the destination image in percent.
  502.      * @param int|bool$size Percentage size of the watermark, false for none.
  503.      * @return void 
  504.      *
  505.      * @throws ezcImageInvalidReferenceException
  506.      *          If no valid resource for the active reference could be found.
  507.      * @throws ezcImageFilterFailedException
  508.      *          If the operation performed by the the filter failed.
  509.      * @throws ezcBaseValueException
  510.      *          If a submitted parameter was out of range or type.
  511.      */
  512.     public function watermarkPercent$image$posX$posY$size false )
  513.     {
  514.         if !is_string$image || !file_exists$image || !is_readable$image ) ) 
  515.         {
  516.             throw new ezcBaseValueException'image'$image'string, path to an image file' );
  517.         }
  518.         if !is_int$posX || $posX || $posX 100 )
  519.         {
  520.             throw new ezcBaseValueException'posX'$posX'int percentage value' );
  521.         }
  522.         if !is_int$posY || $posY || $posY 100 )
  523.         {
  524.             throw new ezcBaseValueException'posY'$posY'int percentage value' );
  525.         }
  526.         if !is_bool$size && !is_int$size || $size || $size 100 ) )
  527.         {
  528.             throw new ezcBaseValueException'size'$size'int percentage value / bool' );
  529.         }
  530.  
  531.         $data getimagesize$this->getReferenceData$this->getActiveReference()"resource" ) );
  532.  
  533.         $originalWidth $data[0];
  534.         $originalHeight $data[1];
  535.  
  536.         $watermarkWidth false;
  537.         $watermarkHeight false;
  538.         
  539.         if $size !== false )
  540.         {
  541.             $watermarkWidth = (int) round$originalWidth $size 100 );
  542.             $watermarkHeight = (int) round$originalHeight $size 100 );
  543.         }
  544.  
  545.         $watermarkPosX = (int) round$originalWidth $posX 100 );
  546.         $watermarkPosY = (int) round$originalHeight $posY 100 );
  547.  
  548.         $this->watermarkAbsolute$image$watermarkPosX$watermarkPosY$watermarkWidth$watermarkHeight );
  549.  
  550.     }
  551.  
  552.     /**
  553.      * Watermark filter.
  554.      * Places a watermark on the image. The file to use as the watermark image
  555.      * is given as $image. The $posX, $posY and $size values are given in
  556.      * pixel. The watermark appear at $posX, $posY in the destination image
  557.      * with a size of $size pixel. If $size is ommitted, the watermark image
  558.      * will not be resized.
  559.      *
  560.      * @param string $image    The image file to use as the watermark
  561.      * @param int $posX        X position in the destination image in pixel.
  562.      * @param int $posY        Y position in the destination image in pixel.
  563.      * @param int|bool$width  Pixel size of the watermark, false to keep size.
  564.      * @param int|bool$height Pixel size of the watermark, false to keep size.
  565.      * @return void 
  566.      *
  567.      * @throws ezcImageInvalidReferenceException
  568.      *          If no valid resource for the active reference could be found.
  569.      * @throws ezcImageFilterFailedException
  570.      *          If the operation performed by the the filter failed.
  571.      * @throws ezcBaseValueException
  572.      *          If a submitted parameter was out of range or type.
  573.      */
  574.     public function watermarkAbsolute$image$posX$posY$width false$height false )
  575.     {
  576.         if !is_string$image || !file_exists$image || !is_readable$image ) )
  577.         {
  578.             throw new ezcBaseValueException'image'$image'string, path to an image file' );
  579.         }
  580.         if !is_int$posX ) )
  581.         {
  582.             throw new ezcBaseValueException'posX'$posX'int' );
  583.         }
  584.         if !is_int$posY ) )
  585.         {
  586.             throw new ezcBaseValueException'posY'$posY'int' );
  587.         }
  588.         if !is_int$width && !is_bool$width ) )
  589.         {
  590.             throw new ezcBaseValueException'width'$width'int/bool' );
  591.         }
  592.         if !is_int$height && !is_bool$height ) )
  593.         {
  594.             throw new ezcBaseValueException'height'$height'int/bool' );
  595.         }
  596.  
  597.         $data getimagesize$this->getActiveResource() );
  598.  
  599.         // Negative offsets
  600.         $posX $posX >= $posX $data[0$posX;
  601.         $posY $posY >= $posY $data[1$posY;
  602.  
  603.         $this->addFilterOption(
  604.             $this->getActiveReference(),
  605.             '-composite',
  606.             '' 
  607.         );
  608.  
  609.         $this->addFilterOption(
  610.             $this->getActiveReference(),
  611.             '-geometry',
  612.             $width !== false $width "" $height !== false "x$height"" "+$posX+$posY"
  613.         );
  614.  
  615.         $this->addCompositeImage$this->getActiveReference()$image );
  616.     }
  617.     
  618.     /**
  619.      * Creates a thumbnail, and crops parts of the given image to fit the range best.
  620.      * This filter creates a thumbnail of the given image. The image is scaled
  621.      * down, keeping the original ratio and keeping the image larger as the
  622.      * given range, if necessary. Overhead for the target range is cropped from
  623.      * both sides equally.
  624.      *
  625.      * If you are looking for a filter that just resizes your image to
  626.      * thumbnail size, you should consider the {@link }
  627.      * ezcImageImagemagickHandler::scale()} filter.
  628.      * 
  629.      * @param int $width  Width of the thumbnail.
  630.      * @param int $height Height of the thumbnail.
  631.      */
  632.     public function croppedThumbnail$width$height )
  633.     {
  634.         if !is_int$width )  || $width )
  635.         {
  636.             throw new ezcBaseValueException'width'$width'int > 0' );
  637.         }
  638.         if !is_int$height )  || $height )
  639.         {
  640.             throw new ezcBaseValueException'height'$height'int > 0' );
  641.         }
  642.         $data getimagesize$this->getReferenceData$this->getActiveReference()"resource" ) );
  643.         
  644.         $scaleRatio  max$width $data[0]$height $data[1);
  645.         $scaleWidth  round$data[0]  $scaleRatio );
  646.         $scaleHeight round$data[1$scaleRatio );
  647.         
  648.         $cropOffsetX $scaleWidth $width )   "+" round( ( $scaleWidth $width )   "+0";
  649.         $cropOffsetY $scaleHeight $height "+" round( ( $scaleHeight $height "+0";
  650.  
  651.         $this->addFilterOption(
  652.             $this->getActiveReference(),
  653.             '-resize',
  654.             $scaleWidth "x" $scaleHeight
  655.         );
  656.         $this->addFilterOption(
  657.             $this->getActiveReference(),
  658.             '-crop',
  659.             $width "x" $height $cropOffsetX $cropOffsetY "!"
  660.         );
  661.     }
  662.  
  663.     /**
  664.      * Creates a thumbnail, and fills up the image to fit the given range.
  665.      * This filter creates a thumbnail of the given image. The image is scaled
  666.      * down, keeping the original ratio and scaling the image smaller as the
  667.      * given range, if necessary. Overhead for the target range is filled with the given
  668.      * color on both sides equally.
  669.      *
  670.      * The color is defined by the following array format (integer values 0-255):
  671.      *
  672.      * <code>
  673.      * array(
  674.      *      0 => <red value>,
  675.      *      1 => <green value>,
  676.      *      2 => <blue value>,
  677.      * );
  678.      * </code>
  679.      *
  680.      * If you are looking for a filter that just resizes your image to
  681.      * thumbnail size, you should consider the {@link }
  682.      * ezcImageImagemagickHandler::scale()} filter.
  683.      * 
  684.      * @param int $width  Width of the thumbnail.
  685.      * @param int $height Height of the thumbnail.
  686.      * @param array $color Fill color.
  687.      * @return void 
  688.      */
  689.     public function filledThumbnail$width$height$color array() )
  690.     {
  691.         if !is_int$width )  || $width )
  692.         {
  693.             throw new ezcBaseValueException'width'$width'int > 0' );
  694.         }
  695.         if !is_int$height )  || $height )
  696.         {
  697.             throw new ezcBaseValueException'height'$height'int > 0' );
  698.         }
  699.         $data getimagesize$this->getReferenceData$this->getActiveReference()"resource" ) );
  700.         
  701.         $scaleRatio  min$width $data[0]$height $data[1);
  702.         $scaleWidth  round$data[0]  $scaleRatio );
  703.         $scaleHeight round$data[1$scaleRatio );
  704.         
  705.         $cropOffsetX $scaleWidth $width )   "-" round( ( $width $scaleWidth )   "+0";
  706.         $cropOffsetY $scaleHeight $height "-" round( ( $height $scaleHeight "+0";
  707.  
  708.         $colorString '#';
  709.         $i 0;
  710.         foreach $color as $id => $colorVal )
  711.         {
  712.             if $i++ > )
  713.             {
  714.                 break;
  715.             }
  716.             if !is_int$colorVal )  || $colorVal || $colorVal 255 )
  717.             {
  718.                 throw new ezcBaseValueException"color[$id]"$color[$id]'int > 0 and < 256' );
  719.             }
  720.             $colorString .= sprintf'%02x'$colorVal );
  721.         }
  722.         
  723.         $this->addFilterOption(
  724.             $this->getActiveReference(),
  725.             '-resize',
  726.             $width "x" $height
  727.         );
  728.         $this->addFilterOption(
  729.             $this->getActiveReference(),
  730.             '-crop',
  731.             $width "x" $height $cropOffsetX $cropOffsetY "!"
  732.         );
  733.         $this->addFilterOption(
  734.             $this->getActiveReference(),
  735.             '-background',
  736.             $colorString
  737.         );
  738.         $this->addFilterOption(
  739.             $this->getActiveReference(),
  740.             '-flatten'
  741.         );
  742.     }
  743.  
  744.     /**
  745.      * Returns the ImageMagick direction modifier for a direction constant.
  746.      * ImageMagick supports the following modifiers to determine if an
  747.      * image should be scaled up only, down only or in both directions:
  748.      *
  749.      * <code>
  750.      *  SCALE_UP:   >
  751.      *  SCALE_DOWN: <
  752.      * </code>
  753.      *
  754.      * This method returns the correct modifier for the internal direction
  755.      * constants.
  756.      *
  757.      * @param int $direction One of ezcImageGeometryFilters::SCALE_*
  758.      * @return string The correct modifier.
  759.      *
  760.      * @throws ezcBaseValueException
  761.      *          If a submitted parameter was out of range or type.
  762.      */
  763.     protected function getDirectionModifier$direction )
  764.     {
  765.         $dirMod '';
  766.         switch $direction )
  767.         {
  768.             case self::SCALE_DOWN:
  769.                 $dirMod '>';
  770.                 break;
  771.             case self::SCALE_UP:
  772.                 $dirMod '<';
  773.                 break;
  774.             case self::SCALE_BOTH:
  775.                 $dirMod '';
  776.                 break;
  777.             default:
  778.                 throw new ezcBaseValueException'direction'$direction'self::SCALE_BOTH, self::SCALE_UP, self::SCALE_DOWN' );
  779.                 break;
  780.         }
  781.         return $dirMod;
  782.     }
  783. }
  784. ?>
Documentation generated by phpDocumentor 1.4.3