resizeExact() публичный Метод

Resize image to exact dimensions ignoring aspect ratio. Useful if you want to force exact width and height.
public resizeExact ( Image &$image, integer $newWidth, integer $newHeight ) : Editor
$image Image
$newWidth integer Width in pixels.
$newHeight integer Height in pixels.
Результат Editor
Пример #1
0
 /**
  * Generate and get the average hash of the image.
  *
  * @param Image $image
  *
  * @param Editor $editor
  *
  * @return string
  */
 public function hash($image, $editor)
 {
     // Resize the image.
     $width = 8;
     $height = 8;
     $image = clone $image;
     // Make sure we are working on the clone if Image is passed
     $editor->resizeExact($image, $width, $height);
     // Resize to exactly 8x8
     $gd = $image->getCore();
     // Create an array of greyscale pixel values.
     $pixels = array();
     for ($y = 0; $y < $height; $y++) {
         for ($x = 0; $x < $width; $x++) {
             $rgba = imagecolorat($gd, $x, $y);
             $r = $rgba >> 16 & 0xff;
             $g = $rgba >> 8 & 0xff;
             $b = $rgba & 0xff;
             $pixels[] = floor(($r + $g + $b) / 3);
             // Gray
         }
     }
     // Get the average pixel value.
     $average = floor(array_sum($pixels) / count($pixels));
     // Each hash bit is set based on whether the current pixels value is above or below the average.
     $hash = '';
     foreach ($pixels as $pixel) {
         if ($pixel > $average) {
             $hash .= '1';
         } else {
             $hash .= '0';
         }
     }
     return $hash;
 }
Пример #2
0
 /**
  * Generate and get the difference hash of image.
  *
  * @param Image $image
  *
  * @param Editor $editor
  *
  * @return string
  */
 public function hash($image, $editor)
 {
     $width = 9;
     $height = 8;
     $image = clone $image;
     // Make sure we are working on the clone if Image is passed
     $editor->resizeExact($image, $width, $height);
     // Resize to exactly 9x8
     $gd = $image->getCore();
     // Build hash
     $hash = '';
     for ($y = 0; $y < $height; $y++) {
         // Get the pixel value for the leftmost pixel.
         $rgba = imagecolorat($gd, 0, $y);
         $r = $rgba >> 16 & 0xff;
         $g = $rgba >> 8 & 0xff;
         $b = $rgba & 0xff;
         $left = floor(($r + $g + $b) / 3);
         for ($x = 1; $x < $width; $x++) {
             // Get the pixel value for each pixel starting from position 1.
             $rgba = imagecolorat($gd, $x, $y);
             $r = $rgba >> 16 & 0xff;
             $g = $rgba >> 8 & 0xff;
             $b = $rgba & 0xff;
             $right = floor(($r + $g + $b) / 3);
             // Each hash bit is set based on whether the left pixel is brighter than the right pixel.
             if ($left > $right) {
                 $hash .= '1';
             } else {
                 $hash .= '0';
             }
             // Prepare the next loop.
             $left = $right;
         }
     }
     $editor->free($image);
     return $hash;
 }