GetPixelColor() public static method

Retourne la couleur d'un pixel dans une image
public static GetPixelColor ( ressource &$img, integer $x, integer $y ) : array | boolean
$img ressource
$x integer
$y integer
return array | boolean
 function GD2BMPstring(&$gd_image)
 {
     $imageX = ImageSX($gd_image);
     $imageY = ImageSY($gd_image);
     $BMP = '';
     for ($y = $imageY - 1; $y >= 0; $y--) {
         $thisline = '';
         for ($x = 0; $x < $imageX; $x++) {
             $argb = phpthumb_functions::GetPixelColor($gd_image, $x, $y);
             $thisline .= chr($argb['blue']) . chr($argb['green']) . chr($argb['red']);
         }
         while (strlen($thisline) % 4) {
             $thisline .= "";
         }
         $BMP .= $thisline;
     }
     $bmpSize = strlen($BMP) + 14 + 40;
     // BITMAPFILEHEADER [14 bytes] - http://msdn.microsoft.com/library/en-us/gdi/bitmaps_62uq.asp
     $BITMAPFILEHEADER = 'BM';
     // WORD    bfType;
     $BITMAPFILEHEADER .= phpthumb_functions::LittleEndian2String($bmpSize, 4);
     // DWORD   bfSize;
     $BITMAPFILEHEADER .= phpthumb_functions::LittleEndian2String(0, 2);
     // WORD    bfReserved1;
     $BITMAPFILEHEADER .= phpthumb_functions::LittleEndian2String(0, 2);
     // WORD    bfReserved2;
     $BITMAPFILEHEADER .= phpthumb_functions::LittleEndian2String(54, 4);
     // DWORD   bfOffBits;
     // BITMAPINFOHEADER - [40 bytes] http://msdn.microsoft.com/library/en-us/gdi/bitmaps_1rw2.asp
     $BITMAPINFOHEADER = phpthumb_functions::LittleEndian2String(40, 4);
     // DWORD  biSize;
     $BITMAPINFOHEADER .= phpthumb_functions::LittleEndian2String($imageX, 4);
     // LONG   biWidth;
     $BITMAPINFOHEADER .= phpthumb_functions::LittleEndian2String($imageY, 4);
     // LONG   biHeight;
     $BITMAPINFOHEADER .= phpthumb_functions::LittleEndian2String(1, 2);
     // WORD   biPlanes;
     $BITMAPINFOHEADER .= phpthumb_functions::LittleEndian2String(24, 2);
     // WORD   biBitCount;
     $BITMAPINFOHEADER .= phpthumb_functions::LittleEndian2String(0, 4);
     // DWORD  biCompression;
     $BITMAPINFOHEADER .= phpthumb_functions::LittleEndian2String(0, 4);
     // DWORD  biSizeImage;
     $BITMAPINFOHEADER .= phpthumb_functions::LittleEndian2String(2835, 4);
     // LONG   biXPelsPerMeter;
     $BITMAPINFOHEADER .= phpthumb_functions::LittleEndian2String(2835, 4);
     // LONG   biYPelsPerMeter;
     $BITMAPINFOHEADER .= phpthumb_functions::LittleEndian2String(0, 4);
     // DWORD  biClrUsed;
     $BITMAPINFOHEADER .= phpthumb_functions::LittleEndian2String(0, 4);
     // DWORD  biClrImportant;
     return $BITMAPFILEHEADER . $BITMAPINFOHEADER . $BMP;
 }
Beispiel #2
0
 public function WhiteBalance(&$gdimg, $targetColor = '')
 {
     if (phpthumb_functions::IsHexColor($targetColor)) {
         $targetPixel = array('red' => hexdec(substr($targetColor, 0, 2)), 'green' => hexdec(substr($targetColor, 2, 2)), 'blue' => hexdec(substr($targetColor, 4, 2)));
     } else {
         $Analysis = phpthumb_filters::HistogramAnalysis($gdimg, false);
         $targetPixel = array('red' => max(array_keys($Analysis['red'])), 'green' => max(array_keys($Analysis['green'])), 'blue' => max(array_keys($Analysis['blue'])));
     }
     $grayValue = phpthumb_functions::GrayscaleValue($targetPixel['red'], $targetPixel['green'], $targetPixel['blue']);
     $scaleR = $grayValue / $targetPixel['red'];
     $scaleG = $grayValue / $targetPixel['green'];
     $scaleB = $grayValue / $targetPixel['blue'];
     for ($x = 0; $x < ImageSX($gdimg); $x++) {
         for ($y = 0; $y < ImageSY($gdimg); $y++) {
             $currentPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y);
             $newColor = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg, max(0, min(255, round($currentPixel['red'] * $scaleR))), max(0, min(255, round($currentPixel['green'] * $scaleG))), max(0, min(255, round($currentPixel['blue'] * $scaleB))), $currentPixel['alpha']);
             ImageSetPixel($gdimg, $x, $y, $newColor);
         }
     }
     return true;
 }
Beispiel #3
0
 function GD2ICOstring(&$gd_image_array)
 {
     foreach ($gd_image_array as $key => $gd_image) {
         $ImageWidths[$key] = ImageSX($gd_image);
         $ImageHeights[$key] = ImageSY($gd_image);
         $bpp[$key] = ImageIsTrueColor($gd_image) ? 32 : 24;
         $totalcolors[$key] = ImageColorsTotal($gd_image);
         $icXOR[$key] = '';
         for ($y = $ImageHeights[$key] - 1; $y >= 0; $y--) {
             for ($x = 0; $x < $ImageWidths[$key]; $x++) {
                 $argb = phpthumb_functions::GetPixelColor($gd_image, $x, $y);
                 $a = round(255 * ((127 - $argb['alpha']) / 127));
                 $r = $argb['red'];
                 $g = $argb['green'];
                 $b = $argb['blue'];
                 if ($bpp[$key] == 32) {
                     $icXOR[$key] .= chr($b) . chr($g) . chr($r) . chr($a);
                 } elseif ($bpp[$key] == 24) {
                     $icXOR[$key] .= chr($b) . chr($g) . chr($r);
                 }
                 if ($a < 128) {
                     @($icANDmask[$key][$y] .= '1');
                 } else {
                     @($icANDmask[$key][$y] .= '0');
                 }
             }
             // mask bits are 32-bit aligned per scanline
             while (strlen($icANDmask[$key][$y]) % 32) {
                 $icANDmask[$key][$y] .= '0';
             }
         }
         $icAND[$key] = '';
         foreach ($icANDmask[$key] as $y => $scanlinemaskbits) {
             for ($i = 0; $i < strlen($scanlinemaskbits); $i += 8) {
                 $icAND[$key] .= chr(bindec(str_pad(substr($scanlinemaskbits, $i, 8), 8, '0', STR_PAD_LEFT)));
             }
         }
     }
     foreach ($gd_image_array as $key => $gd_image) {
         $biSizeImage = $ImageWidths[$key] * $ImageHeights[$key] * ($bpp[$key] / 8);
         // BITMAPINFOHEADER - 40 bytes
         $BitmapInfoHeader[$key] = '';
         $BitmapInfoHeader[$key] .= "(";
         // DWORD  biSize;
         $BitmapInfoHeader[$key] .= phpthumb_functions::LittleEndian2String($ImageWidths[$key], 4);
         // LONG   biWidth;
         // The biHeight member specifies the combined
         // height of the XOR and AND masks.
         $BitmapInfoHeader[$key] .= phpthumb_functions::LittleEndian2String($ImageHeights[$key] * 2, 4);
         // LONG   biHeight;
         $BitmapInfoHeader[$key] .= "";
         // WORD   biPlanes;
         $BitmapInfoHeader[$key] .= chr($bpp[$key]) . "";
         // wBitCount;
         $BitmapInfoHeader[$key] .= "";
         // DWORD  biCompression;
         $BitmapInfoHeader[$key] .= phpthumb_functions::LittleEndian2String($biSizeImage, 4);
         // DWORD  biSizeImage;
         $BitmapInfoHeader[$key] .= "";
         // LONG   biXPelsPerMeter;
         $BitmapInfoHeader[$key] .= "";
         // LONG   biYPelsPerMeter;
         $BitmapInfoHeader[$key] .= "";
         // DWORD  biClrUsed;
         $BitmapInfoHeader[$key] .= "";
         // DWORD  biClrImportant;
     }
     $icondata = "";
     // idReserved;   // Reserved (must be 0)
     $icondata .= "";
     // idType;       // Resource Type (1 for icons)
     $icondata .= phpthumb_functions::LittleEndian2String(count($gd_image_array), 2);
     // idCount;      // How many images?
     $dwImageOffset = 6 + count($gd_image_array) * 16;
     foreach ($gd_image_array as $key => $gd_image) {
         // ICONDIRENTRY   idEntries[1]; // An entry for each image (idCount of 'em)
         $icondata .= chr($ImageWidths[$key]);
         // bWidth;          // Width, in pixels, of the image
         $icondata .= chr($ImageHeights[$key]);
         // bHeight;         // Height, in pixels, of the image
         $icondata .= chr($totalcolors[$key]);
         // bColorCount;     // Number of colors in image (0 if >=8bpp)
         $icondata .= "";
         // bReserved;       // Reserved ( must be 0)
         $icondata .= "";
         // wPlanes;         // Color Planes
         $icondata .= chr($bpp[$key]) . "";
         // wBitCount;       // Bits per pixel
         $dwBytesInRes = 40 + strlen($icXOR[$key]) + strlen($icAND[$key]);
         $icondata .= phpthumb_functions::LittleEndian2String($dwBytesInRes, 4);
         // dwBytesInRes;    // How many bytes in this resource?
         $icondata .= phpthumb_functions::LittleEndian2String($dwImageOffset, 4);
         // dwImageOffset;   // Where in the file is this image?
         $dwImageOffset += strlen($BitmapInfoHeader[$key]);
         $dwImageOffset += strlen($icXOR[$key]);
         $dwImageOffset += strlen($icAND[$key]);
     }
     foreach ($gd_image_array as $key => $gd_image) {
         $icondata .= $BitmapInfoHeader[$key];
         $icondata .= $icXOR[$key];
         $icondata .= $icAND[$key];
     }
     return $icondata;
 }
 function ImageCopyRespectAlpha(&$dst_im, &$src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $opacity_pct = 100)
 {
     $opacipct = $opacity_pct / 100;
     for ($x = $src_x; $x < $src_w; $x++) {
         for ($y = $src_y; $y < $src_h; $y++) {
             $RealPixel = phpthumb_functions::GetPixelColor($dst_im, $dst_x + $x, $dst_y + $y);
             $OverlayPixel = phpthumb_functions::GetPixelColor($src_im, $x, $y);
             $alphapct = $OverlayPixel['alpha'] / 127;
             $overlaypct = (1 - $alphapct) * $opacipct;
             $newcolor = phpthumb_functions::ImageColorAllocateAlphaSafe($dst_im, round($RealPixel['red'] * (1 - $overlaypct)) + $OverlayPixel['red'] * $overlaypct, round($RealPixel['green'] * (1 - $overlaypct)) + $OverlayPixel['green'] * $overlaypct, round($RealPixel['blue'] * (1 - $overlaypct)) + $OverlayPixel['blue'] * $overlaypct, 0);
             ImageSetPixel($dst_im, $dst_x + $x, $dst_y + $y, $newcolor);
         }
     }
     return true;
 }
Beispiel #5
0
 function AlphaChannelFlatten()
 {
     if (!$this->is_alpha) {
         // image doesn't have alpha transparency, no need to flatten
         $this->DebugMessage('skipping AlphaChannelFlatten() because !$this->is_alpha', __FILE__, __LINE__);
         return false;
     }
     switch ($this->thumbnailFormat) {
         case 'png':
         case 'ico':
             // image has alpha transparency, but output as PNG or ICO which can handle it
             $this->DebugMessage('skipping AlphaChannelFlatten() because ($this->thumbnailFormat == "' . $this->thumbnailFormat . '")', __FILE__, __LINE__);
             return false;
             break;
         case 'gif':
             // image has alpha transparency, but output as GIF which can handle only single-color transparency
             $CurrentImageColorTransparent = ImageColorTransparent($this->gdimg_output);
             if ($CurrentImageColorTransparent == -1) {
                 // no transparent color defined
                 if (phpthumb_functions::gd_version() < 2.0) {
                     $this->DebugMessage('AlphaChannelFlatten() failed because GD version is "' . phpthumb_functions::gd_version() . '"', __FILE__, __LINE__);
                     return false;
                 }
                 if ($img_alpha_mixdown_dither = @ImageCreateTrueColor(ImageSX($this->gdimg_output), ImageSY($this->gdimg_output))) {
                     for ($i = 0; $i <= 255; $i++) {
                         $dither_color[$i] = ImageColorAllocate($img_alpha_mixdown_dither, $i, $i, $i);
                     }
                     // scan through current truecolor image copy alpha channel to temp image as grayscale
                     for ($x = 0; $x < $this->thumbnail_width; $x++) {
                         for ($y = 0; $y < $this->thumbnail_height; $y++) {
                             $PixelColor = phpthumb_functions::GetPixelColor($this->gdimg_output, $x, $y);
                             ImageSetPixel($img_alpha_mixdown_dither, $x, $y, $dither_color[$PixelColor['alpha'] * 2]);
                         }
                     }
                     // dither alpha channel grayscale version down to 2 colors
                     ImageTrueColorToPalette($img_alpha_mixdown_dither, true, 2);
                     // reduce color palette to 256-1 colors (leave one palette position for transparent color)
                     ImageTrueColorToPalette($this->gdimg_output, true, 255);
                     // allocate a new color for transparent color index
                     $TransparentColor = ImageColorAllocate($this->gdimg_output, 1, 254, 253);
                     ImageColorTransparent($this->gdimg_output, $TransparentColor);
                     // scan through alpha channel image and note pixels with >50% transparency
                     $TransparentPixels = array();
                     for ($x = 0; $x < $this->thumbnail_width; $x++) {
                         for ($y = 0; $y < $this->thumbnail_height; $y++) {
                             $AlphaChannelPixel = phpthumb_functions::GetPixelColor($img_alpha_mixdown_dither, $x, $y);
                             if ($AlphaChannelPixel['red'] > 127) {
                                 ImageSetPixel($this->gdimg_output, $x, $y, $TransparentColor);
                             }
                         }
                     }
                     ImageDestroy($img_alpha_mixdown_dither);
                     $this->DebugMessage('AlphaChannelFlatten() set image to 255+1 colors with transparency for GIF output', __FILE__, __LINE__);
                     return true;
                 } else {
                     $this->DebugMessage('AlphaChannelFlatten() failed ImageCreate(' . ImageSX($this->gdimg_output) . ', ' . ImageSY($this->gdimg_output) . ')', __FILE__, __LINE__);
                     return false;
                 }
             } else {
                 // a single transparent color already defined, leave as-is
                 $this->DebugMessage('skipping AlphaChannelFlatten() because ($this->thumbnailFormat == "' . $this->thumbnailFormat . '") and ImageColorTransparent returned "' . $CurrentImageColorTransparent . '"', __FILE__, __LINE__);
                 return true;
             }
             break;
     }
     $this->DebugMessage('continuing AlphaChannelFlatten() for output format "' . $this->thumbnailFormat . '"', __FILE__, __LINE__);
     // image has alpha transparency, and is being output in a format that doesn't support it -- flatten
     if ($gdimg_flatten_temp = phpthumb_functions::ImageCreateFunction($this->thumbnail_width, $this->thumbnail_height)) {
         $this->config_background_hexcolor = $this->bg ? $this->bg : $this->config_background_hexcolor;
         if (!phpthumb_functions::IsHexColor($this->config_background_hexcolor)) {
             return $this->ErrorImage('Invalid hex color string "' . $this->config_background_hexcolor . '" for parameter "bg"');
         }
         $background_color = phpthumb_functions::ImageHexColorAllocate($this->gdimg_output, $this->config_background_hexcolor);
         ImageFilledRectangle($gdimg_flatten_temp, 0, 0, $this->thumbnail_width, $this->thumbnail_height, $background_color);
         ImageCopy($gdimg_flatten_temp, $this->gdimg_output, 0, 0, 0, 0, $this->thumbnail_width, $this->thumbnail_height);
         ImageAlphaBlending($this->gdimg_output, true);
         ImageSaveAlpha($this->gdimg_output, false);
         ImageColorTransparent($this->gdimg_output, -1);
         ImageCopy($this->gdimg_output, $gdimg_flatten_temp, 0, 0, 0, 0, $this->thumbnail_width, $this->thumbnail_height);
         ImageDestroy($gdimg_flatten_temp);
         return true;
     } else {
         $this->DebugMessage('ImageCreateFunction() failed', __FILE__, __LINE__);
     }
     return false;
 }
 function Threshold(&$gdimg, $cutoff)
 {
     $cutoff = $cutoff ? $cutoff : 128;
     for ($x = 0; $x < ImageSX($gdimg); $x++) {
         for ($y = 0; $y < ImageSY($gdimg); $y++) {
             $currentPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y);
             $grayPixel = phpthumb_functions::GrayscalePixel($currentPixel);
             if ($grayPixel['red'] < $cutoff) {
                 $newColor = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg, 0x0, 0x0, 0x0, $currentPixel['alpha']);
             } else {
                 $newColor = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg, 0xff, 0xff, 0xff, $currentPixel['alpha']);
             }
             ImageSetPixel($gdimg, $x, $y, $newColor);
         }
     }
     return true;
 }