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

Source for file gd.php

Documentation is available at gd.php

  1. <?php
  2. /**
  3.  * This file contains the ezcImageGdHandler 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 the GD2 extension of PHP, including filters.
  30.  *
  31.  * This ezcImageHandler is used when you want to manipulate images using ext/GD
  32.  * in your application.
  33.  *
  34.  * Note: If you experience problems with loading some JPEG files that work in
  35.  * your image viewer, please set the php.ini directive 'gd.jpeg_ignore_warning'
  36.  * to true (possible via {@link ini_set()}).
  37.  *
  38.  * @see ezcImageConverter
  39.  * @see ezcImageHandler
  40.  *
  41.  * @package ImageConversion
  42.  * @version //autogentag//
  43.  */
  44.                         implements ezcImageGeometryFilters,
  45.                                    ezcImageColorspaceFilters,
  46.                                    ezcImageWatermarkFilters,
  47.                                    ezcImageThumbnailFilters
  48. {
  49.     /**
  50.      * Scale filter.
  51.      * General scale filter. Scales the image to fit into a given box size,
  52.      * determined by a given width and height value, measured in pixel. This
  53.      * method maintains the aspect ratio of the given image. Depending on the
  54.      * given direction value, this method performs the following scales:
  55.      *
  56.      * - ezcImageGeometryFilters::SCALE_BOTH:
  57.      *      The image will be scaled to fit exactly into the given box
  58.      *      dimensions, no matter if it was smaller or larger as the box
  59.      *      before.
  60.      * - ezcImageGeometryFilters::SCALE_DOWN:
  61.      *      The image will be scaled to fit exactly into the given box
  62.      *      only if it was larger than the given box dimensions before. If it
  63.      *      is smaller, the image will not be scaled at all.
  64.      * - ezcImageGeometryFilters::SCALE_UP:
  65.      *      The image will be scaled to fit exactly into the given box
  66.      *      only if it was smaller than the given box dimensions before. If it
  67.      *      is larger, the image will not be scaled at all. ATTENTION:
  68.      *      In this case, the image does not necessarily fit into the given box
  69.      *      afterwards.
  70.      *
  71.      * ATTENTION: Using this filter method directly results in the filter being
  72.      * applied to the image which is internally marked as "active" (most
  73.      * commonly this is the last recently loaded one). It is highly recommended
  74.      * to apply filters through the {@link ezcImageGdHandler::applyFilter()} method,
  75.      * which enables you to specify the image a filter is applied to.
  76.      *
  77.      * @param int $width     Scale to width
  78.      * @param int $height    Scale to height
  79.      * @param int $direction Scale to which direction.
  80.      * @return void 
  81.      *
  82.      * @throws ezcImageInvalidReferenceException
  83.      *          If no valid resource for the active reference could be found.
  84.      * @throws ezcImageFilterFailedException
  85.      *          If the operation performed by the the filter failed.
  86.      * @throws ezcBaseValueException
  87.      *          If a submitted parameter was out of range or type.
  88.      */
  89.     public function scale$width$height$direction ezcImageGeometryFilters::SCALE_BOTH )
  90.     {
  91.  
  92.         if !is_int$width || $width )
  93.         {
  94.             throw new ezcBaseValueException'width'$width'int > 0' );
  95.         }
  96.         if !is_int$height || $height )
  97.         {
  98.             throw new ezcBaseValueException'height'$height'int > 0' );
  99.         }
  100.         
  101.         $resource $this->getActiveResource();
  102.         $oldDim array'x' => imagesx$resource )'y' => imagesy$resource ) );
  103.  
  104.         $widthRatio $width $oldDim['x'];
  105.         $heighRatio $height $oldDim['y'];
  106.         
  107.         $ratio min$widthRatio$heighRatio );
  108.         
  109.         switch $direction )
  110.         {
  111.             case self::SCALE_DOWN:
  112.                 $ratio $ratio $ratio 1;
  113.                 break;
  114.             case self::SCALE_UP:
  115.                 $ratio $ratio $ratio 1;
  116.                 break;
  117.             case self::SCALE_BOTH:
  118.                 break;
  119.             default:
  120.                 throw new ezcBaseValueException'direction'$direction'self::SCALE_BOTH, self::SCALE_UP, self::SCALE_DOWN' );
  121.                 break;
  122.         }
  123.         $this->performScaleround$oldDim['x'$ratio )round$oldDim['y'$ratio ) );
  124.     }
  125.  
  126.     /**
  127.      * Scale after width filter.
  128.      * Scales the image to a give width, measured in pixel. Scales the height
  129.      * automatically while keeping the ratio. The direction dictates, if an
  130.      * image may only be scaled {@link self::SCALE_UP}{@link self::SCALE_DOWN}
  131.      * or if the scale may work in {@link self::SCALE_BOTH} directions.
  132.      *
  133.      * ATTENTION: Using this filter method directly results in the filter being
  134.      * applied to the image which is internally marked as "active" (most
  135.      * commonly this is the last recently loaded one). It is highly recommended
  136.      * to apply filters through the {@link ezcImageGdHandler::applyFilter()} method,
  137.      * which enables you to specify the image a filter is applied to.
  138.      *
  139.      * @param int $width     Scale to width
  140.      * @param int $direction Scale to which direction
  141.      * @return void 
  142.      *
  143.      * @throws ezcImageInvalidReferenceException
  144.      *          If no valid resource for the active reference could be found.
  145.      * @throws ezcImageFilterFailedException
  146.      *          If the operation performed by the the filter failed.
  147.      * @throws ezcBaseValueException
  148.      *          If a submitted parameter was out of range or type.
  149.      */
  150.     public function scaleWidth$width$direction )
  151.     {
  152.         if !is_int$width || $width )
  153.         {
  154.             throw new ezcBaseValueException'width'$width'int > 0' );
  155.         }
  156.  
  157.         $resource $this->getActiveResource();
  158.         $oldDim array(
  159.             'x' => imagesx$resource ),
  160.             'y' => imagesy$resource ),
  161.         );
  162.         switch $direction )
  163.         {
  164.             case self::SCALE_BOTH:
  165.                 $newDim array(
  166.                     'x' => $width,
  167.                     'y' => $width $oldDim['x'$oldDim['y']
  168.                 );
  169.                 break;
  170.             case self::SCALE_UP:
  171.                 $newDim array(
  172.                     'x' => max$width$oldDim['x'),
  173.                     'y' => $width $oldDim['x'round$width $oldDim['x'$oldDim['y'$oldDim['y'],
  174.                 );
  175.                 break;
  176.             case self::SCALE_DOWN:
  177.                 $newDim array(
  178.                     'x' => min$width$oldDim['x'),
  179.                     'y' => $width $oldDim['x'round$width $oldDim['x'$oldDim['y'$oldDim['y'],
  180.                 );
  181.                 break;
  182.             default:
  183.                 throw new ezcBaseValueException'direction'$direction'self::SCALE_BOTH, self::SCALE_UP, self::SCALE_DOWN' );
  184.                 break;
  185.         }
  186.         $this->performScale$newDim["x"]$newDim["y");
  187.     }
  188.  
  189.     /**
  190.      * Scale after height filter.
  191.      * Scales the image to a give height, measured in pixel. Scales the width
  192.      * automatically while keeping the ratio. The direction dictates, if an
  193.      * image may only be scaled {@link self::SCALE_UP}{@link self::SCALE_DOWN}
  194.      * or if the scale may work in {@link self::SCALE_BOTH} directions.
  195.      *
  196.      * ATTENTION: Using this filter method directly results in the filter being
  197.      * applied to the image which is internally marked as "active" (most
  198.      * commonly this is the last recently loaded one). It is highly recommended
  199.      * to apply filters through the {@link ezcImageGdHandler::applyFilter()} method,
  200.      * which enables you to specify the image a filter is applied to.
  201.      *
  202.      * @param int $height    Scale to height
  203.      * @param int $direction Scale to which direction
  204.      * @return void 
  205.      *
  206.      * @throws ezcImageInvalidReferenceException
  207.      *          If no valid resource for the active reference could be found.
  208.      * @throws ezcImageFilterFailedException
  209.      *          If the operation performed by the the filter failed.
  210.      * @throws ezcBaseValueException
  211.      *          If a submitted parameter was out of range or type.
  212.      */
  213.     public function scaleHeight$height$direction )
  214.     {
  215.         if !is_int$height || $height )
  216.         {
  217.             throw new ezcBaseValueException'height'$height'int > 0' );
  218.         }
  219.  
  220.         $resource $this->getActiveResource();
  221.         $oldDim array(
  222.             'x' => imagesx$resource ),
  223.             'y' => imagesy$resource ),
  224.         );
  225.         switch $direction )
  226.         {
  227.             case self::SCALE_BOTH:
  228.                 $newDim array(
  229.                     'x' => $height $oldDim['y'$oldDim['x'],
  230.                     'y' => $height,
  231.                 );
  232.                 break;
  233.             case self::SCALE_UP:
  234.                 $newDim array(
  235.                     'x' => $height $oldDim['y'round$height $oldDim['y'$oldDim['x'$oldDim['x'],
  236.                     'y' => max$height$oldDim['y'),
  237.                 );
  238.                 break;
  239.             case self::SCALE_DOWN:
  240.                 $newDim array(
  241.                     'x' => $height $oldDim['y'round$height $oldDim['y'$oldDim['x'$oldDim['x'],
  242.                     'y' => min$height$oldDim['y'),
  243.                 );
  244.                 break;
  245.             default:
  246.                 throw new ezcBaseValueException'direction'$direction'self::SCALE_BOTH, self::SCALE_UP, self::SCALE_DOWN' );
  247.                 break;
  248.         }
  249.         $this->performScale$newDim["x"]$newDim["y");
  250.     }
  251.  
  252.     /**
  253.      * Scale percent measures filter.
  254.      * Scale an image to a given percentage value size.
  255.      *
  256.      * ATTENTION: Using this filter method directly results in the filter being
  257.      * applied to the image which is internally marked as "active" (most
  258.      * commonly this is the last recently loaded one). It is highly recommended
  259.      * to apply filters through the {@link ezcImageGdHandler::applyFilter()} method,
  260.      * which enables you to specify the image a filter is applied to.
  261.      *
  262.      * @param int $width  Scale to width
  263.      * @param int $height Scale to height
  264.      * @return void 
  265.      *
  266.      * @throws ezcImageInvalidReferenceException
  267.      *          If no valid resource for the active reference could be found.
  268.      * @throws ezcImageFilterFailedException
  269.      *          If the operation performed by the the filter failed.
  270.      * @throws ezcBaseValueException
  271.      *          If a submitted parameter was out of range or type.
  272.      */
  273.     public function scalePercent$width$height )
  274.     {
  275.         if !is_int$height || $height )
  276.         {
  277.             throw new ezcBaseValueException'height'$height'int > 0' );
  278.         }
  279.         if !is_int$width || $width )
  280.         {
  281.             throw new ezcBaseValueException'width'$width'int > 0' );
  282.         }
  283.  
  284.         $resource $this->getActiveResource();
  285.         $this->performScaleroundimagesx$resource $width 100 )roundimagesy$resource $height 100 ) );
  286.     }
  287.  
  288.     /**
  289.      * Scale exact filter.
  290.      * Scale the image to a fixed given pixel size, no matter to which
  291.      * direction.
  292.      *
  293.      * ATTENTION: Using this filter method directly results in the filter being
  294.      * applied to the image which is internally marked as "active" (most
  295.      * commonly this is the last recently loaded one). It is highly recommended
  296.      * to apply filters through the {@link ezcImageGdHandler::applyFilter()} method,
  297.      * which enables you to specify the image a filter is applied to.
  298.      *
  299.      * @param int $width  Scale to width
  300.      * @param int $height Scale to height
  301.      * @return void 
  302.      *
  303.      * @throws ezcImageInvalidReferenceException
  304.      *          If no valid resource for the active reference could be found.
  305.      * @throws ezcBaseValueException
  306.      *          If a submitted parameter was out of range or type.
  307.      */
  308.     public function scaleExact$width$height )
  309.     {
  310.         if !is_int$height || $height )
  311.         {
  312.             throw new ezcBaseValueException'height'$height'int > 0' );
  313.         }
  314.         if !is_int$width || $width )
  315.         {
  316.             throw new ezcBaseValueException'width'$width'int > 0' );
  317.         }
  318.         $this->performScale$width$height );
  319.     }
  320.  
  321.     /**
  322.      * Crop filter.
  323.      * Crop an image to a given size. This takes cartesian coordinates of a
  324.      * rect area to crop from the image. The cropped area will replace the old
  325.      * image resource (not the input image immediately, if you use the
  326.      * {@link ezcImageConverter}).  Coordinates are given as integer values and
  327.      * are measured from the top left corner.
  328.      *
  329.      * ATTENTION: Using this filter method directly results in the filter being
  330.      * applied to the image which is internally marked as "active" (most
  331.      * commonly this is the last recently loaded one). It is highly recommended
  332.      * to apply filters through the {@link ezcImageGdHandler::applyFilter()} method,
  333.      * which enables you to specify the image a filter is applied to.
  334.      *
  335.      * @param int $x      X offset of the cropping area.
  336.      * @param int $y      Y offset of the cropping area.
  337.      * @param int $width  Width of cropping area.
  338.      * @param int $height Height of cropping area.
  339.      * @return void 
  340.      *
  341.      * @throws ezcImageInvalidReferenceException
  342.      *          If no valid resource for the active reference could be found.
  343.      * @throws ezcImageFilterFailedException
  344.      *          If the operation performed by the the filter failed.
  345.      * @throws ezcBaseValueException
  346.      *          If a submitted parameter was out of range or type.
  347.      */
  348.     public function crop$x$y$width$height )
  349.     {
  350.         if !is_int$x ) )
  351.         {
  352.             throw new ezcBaseValueException'x'$x'int' );
  353.         }
  354.         if !is_int$y ) )
  355.         {
  356.             throw new ezcBaseValueException'y'$y'int' );
  357.         }
  358.         if !is_int$height ) )
  359.         {
  360.             throw new ezcBaseValueException'height'$height'int' );
  361.         }
  362.         if !is_int$width ) )
  363.         {
  364.             throw new ezcBaseValueException'width'$width'int' );
  365.         }
  366.         
  367.         $oldResource $this->getActiveResource();
  368.         
  369.         $sourceWidth imagesx$oldResource );
  370.         $sourceHeight imagesy$oldResource );
  371.  
  372.         $x $x >= $x $sourceWidth  $x;
  373.         $y $y >= $y $sourceHeight $y;
  374.         
  375.         $x absmin$x$x $width ) );
  376.         $y absmin$y$y $height ) );
  377.        
  378.         $width abs$width );
  379.         $height abs$height );
  380.  
  381.         if $x $width $sourceWidth )
  382.         {
  383.             $width $sourceWidth $x;
  384.         }
  385.         if $y $height $sourceHeight )
  386.         {
  387.             $height $sourceHeight $y;
  388.         }
  389.         
  390.         $this->performCrop$x$y$width$height );
  391.  
  392.     }
  393.  
  394.     /**
  395.      * Colorspace filter.
  396.      * Transform the colorspace of the picture. The following colorspaces are
  397.      * supported:
  398.      *
  399.      * - {@link self::COLORSPACE_GREY} - 255 grey colors
  400.      * - {@link self::COLORSPACE_SEPIA} - Sepia colors
  401.      * - {@link self::COLORSPACE_MONOCHROME} - 2 colors black and white
  402.      *
  403.      * ATTENTION: Using this filter method directly results in the filter being
  404.      * applied to the image which is internally marked as "active" (most
  405.      * commonly this is the last recently loaded one). It is highly recommended
  406.      * to apply filters through the {@link ezcImageGdHandler::applyFilter()} method,
  407.      * which enables you to specify the image a filter is applied to.
  408.      *
  409.      * @param int $space Colorspace, one of self::COLORSPACE_* constants.
  410.      *
  411.      * @throws ezcImageInvalidReferenceException
  412.      *          If no valid resource for the active reference could be found.
  413.      * @throws ezcBaseValueException
  414.      *          If the parameter submitted as the colorspace was not within the
  415.      *          self::COLORSPACE_* constants.
  416.      * @throws ezcImageFilterFailedException
  417.      *          If the operation performed by the the filter failed.
  418.      */
  419.     public function colorspace$space )
  420.     {
  421.         switch $space )
  422.         {
  423.             case self::COLORSPACE_GREY:
  424.                 $this->luminanceColorScalearray1.01.01.0 ) );
  425.                 break;
  426.             case self::COLORSPACE_MONOCHROME:
  427.                 $this->thresholdColorScale(
  428.                     array(
  429.                         127 => array00),
  430.                         255 => array255255255 ),
  431.                     )
  432.                 );
  433.                 break;
  434.             return;
  435.             case self::COLORSPACE_SEPIA:
  436.                 $this->luminanceColorScalearray1.00.890.74 ) );
  437.                 break;
  438.             default:
  439.                 throw new ezcBaseValueException'space'$space'self::COLORSPACE_GREY, self::COLORSPACE_SEPIA, self::COLORSPACE_MONOCHROME' );
  440.                 break;
  441.         }
  442.  
  443.     }
  444.     
  445.     /**
  446.      * Watermark filter.
  447.      * Places a watermark on the image. The file to use as the watermark image
  448.      * is given as $image. The $posX, $posY and $size values are given in
  449.      * percent, related to the destination image. A $size value of 10 will make
  450.      * the watermark appear in 10% of the destination image size.
  451.      * $posX = $posY = 10 will make the watermark appear in the top left corner
  452.      * of the destination image, 10% of its size away from its borders. If
  453.      * $size is ommitted, the watermark image will not be resized.
  454.      *
  455.      * @param string $image  The image file to use as the watermark
  456.      * @param int $posX      X position in the destination image in percent.
  457.      * @param int $posY      Y position in the destination image in percent.
  458.      * @param int|bool$size Percentage size of the watermark, false for none.
  459.      * @return void 
  460.      *
  461.      * @throws ezcImageInvalidReferenceException
  462.      *          If no valid resource for the active reference could be found.
  463.      * @throws ezcImageFilterFailedException
  464.      *          If the operation performed by the the filter failed.
  465.      * @throws ezcBaseValueException
  466.      *          If a submitted parameter was out of range or type.
  467.      */
  468.     public function watermarkPercent$image$posX$posY$size false )
  469.     {
  470.         if !is_string$image || !file_exists$image || !is_readable$image ) ) 
  471.         {
  472.             throw new ezcBaseValueException'image'$image'string, path to an image file' );
  473.         }
  474.         if !is_int$posX || $posX || $posX 100 )
  475.         {
  476.             throw new ezcBaseValueException'posX'$posX'int percentage value' );
  477.         }
  478.         if !is_int$posY || $posY || $posY 100 )
  479.         {
  480.             throw new ezcBaseValueException'posY'$posY'int percentage value' );
  481.         }
  482.         if !is_bool$size && !is_int$size || $size || $size 100 ) )
  483.         {
  484.             throw new ezcBaseValueException'size'$size'int percentage value / bool' );
  485.         }
  486.         
  487.         $imgWidth imagesx$this->getActiveResource() );
  488.         $imgHeight imagesy$this->getActiveResource() );
  489.  
  490.         $watermarkWidth false;
  491.         $watermarkHeight false;
  492.         if $size !== false )
  493.         {
  494.             $watermarkWidth = (int) round$imgWidth $size 100 );
  495.             $watermarkHeight = (int) round$imgHeight $size 100 );
  496.         }
  497.  
  498.         $watermarkPosX = (int) round$imgWidth $posX 100 );
  499.         $watermarkPosY = (int) round$imgHeight $posY 100 );
  500.  
  501.         $this->watermarkAbsolute$image$watermarkPosX$watermarkPosY$watermarkWidth$watermarkHeight );
  502.     }
  503.  
  504.     /**
  505.      * Watermark filter.
  506.      * Places a watermark on the image. The file to use as the watermark image
  507.      * is given as $image. The $posX, $posY and $size values are given in
  508.      * pixel. The watermark appear at $posX, $posY in the destination image
  509.      * with a size of $size pixel. If $size is ommitted, the watermark image
  510.      * will not be resized.
  511.      *
  512.      * @param string $image    The image file to use as the watermark
  513.      * @param int $posX        X position in the destination image in pixel.
  514.      * @param int $posY        Y position in the destination image in pixel.
  515.      * @param int|bool$width  Pixel size of the watermark, false to keep size.
  516.      * @param int|bool$height Pixel size of the watermark, false to keep size.
  517.      * @return void 
  518.      *
  519.      * @throws ezcImageInvalidReferenceException
  520.      *          If no valid resource for the active reference could be found.
  521.      * @throws ezcImageFilterFailedException
  522.      *          If the operation performed by the the filter failed.
  523.      * @throws ezcBaseValueException
  524.      *          If a submitted parameter was out of range or type.
  525.      * @throws ezcImageFileNotProcessableException
  526.      *          If the given watermark image could not be loaded.
  527.      */
  528.     public function watermarkAbsolute$image$posX$posY$width false$height false )
  529.     {
  530.         if !is_string$image || !file_exists$image || !is_readable$image ) )
  531.         {
  532.             throw new ezcBaseValueException'image'$image'string, path to an image file' );
  533.         }
  534.         if !is_int$posX ) )
  535.         {
  536.             throw new ezcBaseValueException'posX'$posX'int' );
  537.         }
  538.         if !is_int$posY ) )
  539.         {
  540.             throw new ezcBaseValueException'posY'$posY'int' );
  541.         }
  542.         if !is_int$width && !is_bool$width ) )
  543.         {
  544.             throw new ezcBaseValueException'width'$width'int/bool' );
  545.         }
  546.         if !is_int$height && !is_bool$height ) )
  547.         {
  548.             throw new ezcBaseValueException'height'$height'int/bool' );
  549.         }
  550.         
  551.         // Backup original image reference
  552.         $originalRef $this->getActiveReference();
  553.  
  554.         $originalWidth  imagesx$this->getActiveResource() );
  555.         $originalHeight imagesy$this->getActiveResource() );
  556.  
  557.         $watermarkRef $this->load$image );
  558.         if $width !== false && $height !== false && $originalWidth !== $width || $originalHeight !== $height ) )
  559.         {
  560.             $this->scale$width$heightezcImageGeometryFilters::SCALE_BOTH );
  561.         }
  562.  
  563.         // Negative offsets
  564.         $posX $posX >= $posX $originalWidth  $posX;
  565.         $posY $posY >= $posY $originalHeight $posY;
  566.  
  567.         imagecopy(
  568.             $this->getReferenceData$originalRef"resource" ),                // resource $dst_im
  569.             $this->getReferenceData$watermarkRef"resource" ),               // resource $src_im
  570.             $posX,                                                              // int $dst_x
  571.             $posY,                                                              // int $dst_y
  572.             0,                                                                  // int $src_x
  573.             0,                                                                  // int $src_y
  574.             imagesx$this->getReferenceData$watermarkRef"resource" ) ),    // int $src_w
  575.             imagesy$this->getReferenceData$watermarkRef"resource" ) )     // int $src_h
  576.         );
  577.  
  578.         $this->close$watermarkRef );
  579.         
  580.         // Restore original image reference
  581.         $this->setActiveReference$originalRef );
  582.     }
  583.     
  584.     /**
  585.      * Creates a thumbnail, and crops parts of the given image to fit the range best.
  586.      * This filter creates a thumbnail of the given image. The image is scaled
  587.      * down, keeping the original ratio and keeping the image larger as the
  588.      * given range, if necessary. Overhead for the target range is cropped from
  589.      * both sides equally.
  590.      *
  591.      * If you are looking for a filter that just resizes your image to
  592.      * thumbnail size, you should consider the {@link }
  593.      * ezcImageGdHandler::scale()} filter.
  594.      * 
  595.      * @param int $width  Width of the thumbnail.
  596.      * @param int $height Height of the thumbnail.
  597.      */
  598.     public function croppedThumbnail$width$height )
  599.     {
  600.         if !is_int$width )  || $width )
  601.         {
  602.             throw new ezcBaseValueException'width'$width'int > 0' );
  603.         }
  604.         if !is_int$height )  || $height )
  605.         {
  606.             throw new ezcBaseValueException'height'$height'int > 0' );
  607.         }
  608.         $resource $this->getActiveResource();
  609.         $data[0imagesx$resource );
  610.         $data[1imagesy$resource );
  611.         
  612.         $scaleRatio  max$width $data[0]$height $data[1);
  613.         $scaleWidth  round$data[0$scaleRatio );
  614.         $scaleHeight round$data[1$scaleRatio );
  615.         
  616.         $cropOffsetX $scaleWidth $width )   round( ( $scaleWidth $width )   0;
  617.         $cropOffsetY $scaleHeight $height round( ( $scaleHeight $height 0;
  618.  
  619.         $this->performScale$scaleWidth$scaleHeight );
  620.         $this->performCrop$cropOffsetX$cropOffsetY$width$height );
  621.     }
  622.  
  623.     /**
  624.      * Creates a thumbnail, and fills up the image to fit the given range.
  625.      * This filter creates a thumbnail of the given image. The image is scaled
  626.      * down, keeping the original ratio and scaling the image smaller as the
  627.      * given range, if necessary. Overhead for the target range is filled with the given
  628.      * color on both sides equally.
  629.      *
  630.      * The color is defined by the following array format (integer values 0-255):
  631.      *
  632.      * <code>
  633.      * array(
  634.      *      0 => <red value>,
  635.      *      1 => <green value>,
  636.      *      2 => <blue value>,
  637.      * );
  638.      * </code>
  639.      *
  640.      * If you are looking for a filter that just resizes your image to
  641.      * thumbnail size, you should consider the {@link }
  642.      * ezcImageGdHandler::scale()} filter.
  643.      * 
  644.      * @param int $width  Width of the thumbnail.
  645.      * @param int $height Height of the thumbnail.
  646.      * @param array $color Fill color.
  647.      *
  648.      * @return void 
  649.      */
  650.     public function filledThumbnail$width$height$color array() )
  651.     {
  652.         $i 0;
  653.         foreach $color as $id => $colorVal )
  654.         {
  655.             if $i++ > )
  656.             {
  657.                 break;
  658.             }
  659.             if !is_int$colorVal )  || $colorVal || $colorVal 255 )
  660.             {
  661.                 throw new ezcBaseValueException"color[$id]"$color[$id]'int > 0 and < 256' );
  662.             }
  663.         }
  664.         
  665.         // Sanity checks for $width and $height performed by scale() method.
  666.         $this->scale$width$heightezcImageGeometryFilters::SCALE_BOTH );
  667.         
  668.         $oldResource $this->getActiveResource();
  669.  
  670.         $realWidth   imagesx$oldResource );
  671.         $realHeight  imagesy$oldResource );
  672.         $xOffset     $width $realWidth )   round( ( $width $realWidth )   0;
  673.         $yOffset     $height $realHeight round( ( $height $realHeight 0;
  674.  
  675.         $newResource imagecreatetruecolor$width$height );
  676.         $bgColor     $this->getColor$newResource$color[0]$color[1]$color[2);
  677.         if imagefill$newResource00$bgColor === false )
  678.         {
  679.             throw new ezcImageFilterFailedException"filledThumbnail""Color fill failed." );
  680.         }
  681.  
  682.         imagecopy(
  683.             $newResource,
  684.             $oldResource,
  685.             $xOffset,
  686.             $yOffset,
  687.             0,
  688.             0,
  689.             $realWidth,
  690.             $realHeight
  691.         );
  692.  
  693.         $this->setActiveResource$newResource );
  694.         imagedestroy$oldResource );
  695.     }
  696.  
  697.     // private
  698.  
  699.     /**
  700.      * Retrieve luminance value for a specific pixel.
  701.      *
  702.      * @param resource(GD) $resource Image resource
  703.      * @param int $x                 Pixel x coordinate.
  704.      * @param int $y                 Pixel y coordinate.
  705.      * @return float Luminance value.
  706.      */
  707.     private function getLuminanceAt$resource$x$y )
  708.     {
  709.             $currentColor imagecolorat$resource$x$y );
  710.             $rgbValues array(
  711.                 'r' => $currentColor >> 16 0xff,
  712.                 'g' => $currentColor >> 0xff,
  713.                 'b' => $currentColor 0xff,
  714.             );
  715.             return $rgbValues['r'0.299 $rgbValues['g'0.587 $rgbValues['b'0.114;
  716.     }
  717.  
  718.     /**
  719.      * Scale colors by threshold values.
  720.      * Thresholds are defined by the following array structures:
  721.      *
  722.      * <code>
  723.      * array(
  724.      *  <int threshold value> => array(
  725.      *      0 => <int red value>,
  726.      *      1 => <int green value>,
  727.      *      2 => <int blue value>,
  728.      *  ),
  729.      * )
  730.      * </code>
  731.      *
  732.      * @param array $thresholds 
  733.      * @return void 
  734.      *
  735.      * @throws ezcImageInvalidReferenceException
  736.      *          If no valid resource for the active reference could be found.
  737.      * @throws ezcImageFilterFailedException
  738.      *          If the operation performed by the the filter failed.
  739.      *
  740.      * @todo Optimization as described here: http://lists.ez.no/pipermail/components/2005-November/000566.html
  741.      */
  742.     protected function thresholdColorScale$thresholds )
  743.     {
  744.         $resource $this->getActiveResource();
  745.         $dimensions array'x' => imagesx$resource )'y' => imagesy$resource ) );
  746.  
  747.         // Check for GIFs and convert them to work properly here.
  748.         if !imageistruecolor$resource ) )
  749.         {
  750.             $resource $this->paletteToTruecolor$resource$dimensions );
  751.         }
  752.  
  753.         foreach $thresholds as $threshold => $colors )
  754.         {
  755.             $thresholds[$thresholdarray_merge(
  756.                 $colors,
  757.                 array'color' => $this->getColor$resource$colors[0]$colors[1]$colors[2) )
  758.             );
  759.         }
  760.         // Default
  761.         if !isset$thresholds[255) )
  762.         {
  763.             $thresholds[255end$thresholds );
  764.             reset$thresholds );
  765.         }
  766.  
  767.         $colorCache array();
  768.  
  769.         for $x 0$x $dimensions['x']$x++ )
  770.         {
  771.             for $y 0$y $dimensions['y']$y++ )
  772.             {
  773.                 $luminance $this->getLuminanceAt$resource$x$y );
  774.                 $color end$thresholds );
  775.                 foreach $thresholds as $threshold => $colorValues )
  776.                 {
  777.                     if $luminance <= $threshold )
  778.                     {
  779.                         $color $colorValues;
  780.                         break;
  781.                     }
  782.                 }
  783.                 imagesetpixel$resource$x$y$color['color');
  784.             }
  785.         }
  786.  
  787.         $this->setActiveResource$resource );
  788.     }
  789.  
  790.     /**
  791.      * Perform luminance color scale.
  792.      *
  793.      * @param array $scale Array of RGB values (numeric index).
  794.      *
  795.      * @throws ezcImageInvalidReferenceException
  796.      *          If no valid resource for the active reference could be found.
  797.      * @throws ezcImageFilterFailedException
  798.      *          If the operation performed by the the filter failed
  799.      *
  800.      * @todo Optimization as described here: http://lists.ez.no/pipermail/components/2005-November/000566.html
  801.      */
  802.     protected function luminanceColorScale$scale )
  803.     {
  804.         $resource $this->getActiveResource();
  805.         $dimensions array'x' => imagesx$resource )'y' => imagesy$resource ) );
  806.  
  807.         // Check for GIFs and convert them to work properly here.
  808.         if !imageistruecolor$resource ) )
  809.         {
  810.             $resource $this->paletteToTruecolor$resource$dimensions );
  811.         }
  812.  
  813.         for $x 0$x $dimensions['x']$x++ )
  814.         {
  815.             for $y 0$y $dimensions['y']$y++ )
  816.             {
  817.                 $luminance $this->getLuminanceAt$resource$x$y );
  818.                 $newRgbValues array(
  819.                     'r' => $luminance $scale[0],
  820.                     'g' => $luminance $scale[1],
  821.                     'b' => $luminance $scale[2],
  822.                 );
  823.                 $color $this->getColor$resource$newRgbValues['r']$newRgbValues['g']$newRgbValues['b');
  824.                 imagesetpixel$resource$x$y$color );
  825.             }
  826.         }
  827.         $this->setActiveResource$resource );
  828.     }
  829.  
  830.     /**
  831.      * Convert a palette based image resource to a true color one.
  832.      * Takes a GD resource that does not represent a true color image and
  833.      * converts it to a true color based resource. Do not forget, to replace
  834.      * the actual resource in the handler, if you use this ,method!
  835.      *
  836.      * @param resource(GD) $resource         The image resource to convert
  837.      * @param array(string=>int) $dimensions X and Y dimensions.
  838.      * @return resource(GD) The converted resource.
  839.      */
  840.     protected function paletteToTruecolor$resource$dimensions )
  841.     {
  842.         $newResource imagecreatetruecolor$dimensions['x']$dimensions['y');
  843.         imagecopy$newResource$resource0000$dimensions['x']$dimensions['y');
  844.         imagedestroy$resource );
  845.         return $newResource;
  846.     }
  847.  
  848.     /**
  849.      * Common color determination method.
  850.      * Returns a color identifier for an RGB value. Avoids problems with palette images.
  851.      *
  852.      * @param reource(GD) $resource The image resource to get a color for.
  853.      * @param int $r                Red value.
  854.      * @param int $g                Green value.
  855.      * @param int $b                Blue value.
  856.      *
  857.      * @return int The color identifier.
  858.      *
  859.      * @throws ezcImageFilterFailedException
  860.      *          If the operation performed by the the filter failed.
  861.      */
  862.     protected function getColor$resource$r$g$b )
  863.     {
  864.         if ( ( $res imagecolorexact$resource$r$g$b ) ) !== -)
  865.         {
  866.             return $res;
  867.         }
  868.         if ( ( $res imagecolorallocate$resource$r$g$b ) ) !== -)
  869.         {
  870.             return $res;
  871.         }
  872.         if ( ( $res imagecolorclosest$resource$r$g$b ) ) !== -)
  873.         {
  874.             return $res;
  875.         }
  876.         throw new ezcImageFilterFailedException'allocatecolor'"Color allocation failed for color r: '{$r}', g: '{$g}', b: '{$b}'.);
  877.     }
  878.  
  879.     /**
  880.      * General scaling method to perform actual scale to new dimensions.
  881.      *
  882.      * @param int $width  Width.
  883.      * @param int $height Height.
  884.      * @return void 
  885.      *
  886.      * @throws ezcImageInvalidReferenceException
  887.      *          If no valid resource for the active reference could be found.
  888.      * @throws ezcImageFilterFailedException.
  889.      *          If the operation performed by the the filter failed.
  890.      */
  891.     protected function performScale$width$height )
  892.     {
  893.         $oldResource $this->getActiveResource();
  894.         if imageistruecolor$oldResource ) )
  895.         {
  896.             $newResource imagecreatetruecolor$width$height );
  897.         }
  898.         else
  899.         {
  900.             $newResource imagecreate$width$height );
  901.         }
  902.    
  903.         // Save transparency, if image has it
  904.         $bgColor imagecolorallocatealpha(  $newResource255255255127 );
  905.         imagealphablending(  $newResourcetrue );
  906.         imagesavealpha(  $newResourcetrue );
  907.         imagefill(  $newResource11$bgColor );
  908.  
  909.         $res imagecopyresampled(
  910.             $newResource,
  911.             $oldResource,
  912.             0000,
  913.             $width,
  914.             $height,
  915.             imagesx$this->getActiveResource() ),
  916.             imagesy$this->getActiveResource() )
  917.         );
  918.         if $res === false )
  919.         {
  920.             throw new ezcImageFilterFailedException'scale''Resampling of image failed.' );
  921.         }
  922.         imagedestroy$oldResource );
  923.         $this->setActiveResource$newResource );
  924.     }
  925.  
  926.     /**
  927.      * General method to perform a crop operation.
  928.      * 
  929.      * @param int $x 
  930.      * @param int $y 
  931.      * @param int $width 
  932.      * @param int $height 
  933.      * @return void 
  934.      */
  935.     private function performCrop$x$y$width$height )
  936.     {
  937.         $oldResource $this->getActiveResource();
  938.         if imageistruecolor$oldResource ) )
  939.         {
  940.             $newResource imagecreatetruecolor$width$height  );
  941.         }
  942.         else
  943.         {
  944.             $newResource imagecreate$width$height );
  945.         }
  946.         
  947.         // Save transparency, if image has it
  948.         $bgColor imagecolorallocatealpha(  $newResource255255255127 );
  949.         imagealphablending(  $newResourcetrue );
  950.         imagesavealpha(  $newResourcetrue );
  951.         imagefill(  $newResource11$bgColor );
  952.         
  953.         $res imagecopyresampled(
  954.             $newResource,           // destination resource 
  955.             $oldResource,           // source resource
  956.             0,                      // destination x coord
  957.             0,                      // destination y coord
  958.             $x,                     // source x coord
  959.             $y,                     // source y coord
  960.             $width,                 // destination width
  961.             $height,                // destination height
  962.             $width,                 // source witdh
  963.             $height                 // source height
  964.         );
  965.         if $res === false )
  966.         {
  967.             throw new ezcImageFilterFailedException'crop''Resampling of image failed.' );
  968.         }
  969.         imagedestroy$oldResource );
  970.         $this->setActiveResource$newResource );
  971.     }
  972. }
  973. ?>
Documentation generated by phpDocumentor 1.4.3