Example #1
0
 /**
  * @param resource $image GD image resource
  * @return int Returns the index of the specified color+alpha in the palette of the image,
  *             or -1 if the color does not exist in the image's palette.
  */
 public function getIndex($image)
 {
     if ($this->hasAlphaChannel()) {
         return imagecolorexactalpha($image, $this->red, $this->green, $this->blue, $this->alpha);
     } else {
         return imagecolorexact($image, $this->red, $this->green, $this->blue);
     }
 }
Example #2
0
 protected function getColorIndex($v)
 {
     if (count($v) == 3) {
         $color = imagecolorexact($this->im, $v[0], $v[1], $v[2]);
     } elseif (count($v) == 4) {
         $color = imagecolorexactalpha($this->im, $v[0], $v[1], $v[2], $v[3]);
     }
     return $color;
 }
Example #3
0
function rgb_allocate($image, $color)
{
    list($r, $g, $b) = rgb_color($color);
    $index = imagecolorexact($image, $r, $g, $b);
    if ($index == -1) {
        return imagecolorallocate($image, $r, $g, $b);
    } else {
        return $index;
    }
}
Example #4
0
 protected function color(array $color, $image = false)
 {
     if (!is_array($image)) {
         $image = $this->map;
     }
     list($r, $g, $b) = $color;
     $colorRes = imagecolorexact($image['image'], $r, $g, $b);
     if ($colorRes == -1) {
         $colorRes = imageColorAllocate($image['image'], $r, $g, $b);
     }
     return $colorRes;
 }
Example #5
0
 public function toPngColor($img)
 {
     $color = imagecolorexact($img, $this->red, $this->green, $this->blue);
     if ($color == -1) {
         if (imagecolorstotal($img) >= 255) {
             $color = imagecolorclosest($img, $this->red, $this->green, $this->blue);
         } else {
             $color = imagecolorallocate($img, $this->red, $this->green, $this->blue);
         }
     }
     return $color;
 }
Example #6
0
 function _get_image_color($im, $r, $g, $b)
 {
     $c = imagecolorexact($im, $r, $g, $b);
     if ($c != -1) {
         return $c;
     }
     $c = imagecolorallocate($im, $r, $g, $b);
     if ($c != -1) {
         return $c;
     }
     return imagecolorclosest($im, $r, $g, $b);
 }
Example #7
0
 public function drawQR()
 {
     if (!($qr = $this->getQR())) {
         return false;
     }
     $this->_qrW = imagesx($qr);
     $this->_qrH = imagesy($qr);
     imagecolortransparent($qr, imagecolorexact($qr, 255, 255, 255));
     imagecopy($this->getImg(), $qr, $this->_contentStartX, $this->_contentStartY, 0, 0, $this->_qrW, $this->_qrH);
     //space after qr
     $this->_qrW += 71;
     return true;
 }
Example #8
0
 function setup()
 {
     $this->map = array();
     $nthis->umStarts = 0;
     $this->numBases = 0;
     $this->numPills = 0;
     $this->img = ImageCreate(255, 255);
     $this->pens = array();
     $this->pens[0] = ImageColorAllocate($this->img, 179, 121, 15);
     /* BUILDING */
     $this->pens[1] = ImageColorAllocate($this->img, 0, 255, 255);
     /* RIVER */
     $this->pens[2] = ImageColorAllocate($this->img, 0, 128, 128);
     /* SWAMP */
     $this->pens[3] = ImageColorAllocate($this->img, 128, 64, 0);
     /* CRATER */
     $this->pens[4] = ImageColorAllocate($this->img, 0, 0, 0);
     /* ROAD */
     $this->pens[5] = ImageColorAllocate($this->img, 0, 128, 0);
     /* FOREST */
     $this->pens[6] = ImageColorAllocate($this->img, 234, 165, 123);
     /* RUBBLE */
     $this->pens[7] = ImageColorAllocate($this->img, 0, 255, 0);
     /* GRASS */
     $this->pens[8] = ImageColorAllocate($this->img, 223, 152, 19);
     /* SHOT_BUILDING */
     $this->pens[9] = ImageColorAllocate($this->img, 0, 0, 121);
     /* BOAT */
     $this->pens[10] = ImageColorAllocate($this->img, 0, 128, 128);
     /* SWAMP + MINE */
     $this->pens[11] = ImageColorAllocate($this->img, 128, 64, 0);
     /* CRATER_MINE */
     $this->pens[12] = ImageColorAllocate($this->img, 0, 0, 0);
     /* ROAD_MINE  */
     $this->pens[13] = ImageColorAllocate($this->img, 0, 128, 0);
     /* FOREST_MINE */
     $this->pens[14] = ImageColorAllocate($this->img, 234, 165, 123);
     /* RUBBLE_MINE */
     $this->pens[15] = ImageColorAllocate($this->img, 0, 255, 0);
     /* GrassMine */
     $this->pens[16] = ImageColorAllocate($this->img, 0, 0, 255);
     /* DEEP_SEA */
     $this->pens[18] = ImageColorAllocate($this->img, 255, 0, 0);
     /* PILLBOX */
     $this->pens[17] = ImageColorAllocate($this->img, 255, 255, 0);
     /* BASE */
     $this->pens[19] = ImageColorAllocate($this->img, 128, 128, 128);
     /* START */
     ImageFilledRectangle($this->img, 0, 0, 255, 255, $this->pens[16]);
     $this->water = imagecolorexact($this->img, 0, 0, 255);
 }
function myimagecolorallocate($image, $red, $green, $blue)
{
    // it's possible that we're being called early - just return straight away, in that case
    if (!isset($image)) {
        return -1;
    }
    if (1 == 0) {
        $existing = imagecolorexact($image, $red, $green, $blue);
        if ($existing > -1) {
            return $existing;
        }
    }
    return imagecolorallocate($image, $red, $green, $blue);
}
function set_reasonable_image_size($image, $filename)
{
	global $CONF;
	list($width, $height) = getimagesize($filename);
	if ($width <= $CONF['user_temp_avatar_max_width'] && $height <= $CONF['user_temp_avatar_max_height'])
		return $image;
	$size = resize_dimensions($CONF['user_temp_avatar_max_width'],$CONF['user_temp_avatar_max_height'],$width,$height);

	$fwidth = $size['width'];
	$fheight = $size['height'];

	$thumb = imagecreatetruecolor($fwidth, $fheight); setTransparency($thumb,$image); 
	$index = imagecolorexact($thumb, 255, 255, 255); imagecolortransparent($thumb, $index);
	imagecopyresampled($thumb, $image, 0, 0, 0, 0, $fwidth, $fheight, $width, $height);
	imagepng($thumb,$filename);
	return $thumb;
}
 /**
  * Insert a color to the palet or return the color if the color exist
  *
  * @param image $pic the picture where we work on it
  * @param int $c1 red part of the color
  * @param int $c2 green part of the color
  * @param int $c3 blue part of the color
  * @return color the color that we want
  */
 public static function createcolor($pic, $c1, $c2, $c3)
 {
     //get color from palette
     $color = imagecolorexact($pic, $c1, $c2, $c3);
     if ($color == -1) {
         //color does not exist...
         //test if we have used up palette
         if (imagecolorstotal($pic) >= 255) {
             //palette used up; pick closest assigned color
             $color = imagecolorclosest($pic, $c1, $c2, $c3);
         } else {
             //palette NOT used up; assign new color
             $color = imagecolorallocate($pic, $c1, $c2, $c3);
         }
     }
     return $color;
 }
function img2array($im, $width, $height)
{
    $black = abs(intval(imagecolorexact($im, 0, 0, 0)));
    $white = imagecolorexact($im, 255, 255, 255);
    $imgarray = array();
    for ($x = 0; $x < $width; $x++) {
        for ($y = 0; $y < $height; $y++) {
            // echo imagecolorat($im,$x,$y).'<br />';
            if (imagecolorat($im, $x, $y) == $black) {
                $imgarray[$y][$x] = 1;
            } else {
                $imgarray[$y][$x] = 0;
            }
            //			$imgarray[$y][$x] = imagecolorat($im,$x,$y);
        }
    }
    return $imgarray;
}
function update_user_avatar($file, $x1, $y1, $x2, $y2){

	//file_put_contents('aa.txt',$file);
	preg_match('/\/(\d+\.png)$/', $file, $matches); // this is for security reasons
	$file = 'imgs/temp/'.$matches[1];

	global $_FILES;
	global $_USER;
	global $CONF;

	$user = $_SESSION['user'];

	$result='ok';

	$image = imagecreatefrompng($file);

	$thumbb = imagecreatetruecolor($CONF['user_avatar_width_big'], $CONF['user_avatar_width_big']); setTransparency($thumbb,$image); 
	$thumbm = imagecreatetruecolor($CONF['user_avatar_width_med'], $CONF['user_avatar_width_med']);	setTransparency($thumbm,$image); 
	$thumbs = imagecreatetruecolor($CONF['user_avatar_width_small'], $CONF['user_avatar_width_small']);setTransparency($thumbs,$image); 

	$index = imagecolorexact($thumbb, 255, 255, 255); imagecolortransparent($thumbb, $index);
	$index = imagecolorexact($thumbm, 255, 255, 255); imagecolortransparent($thumbm, $index);
	$index = imagecolorexact($thumbs, 255, 255, 255); imagecolortransparent($thumbs, $index);

	imagecopyresampled($thumbb, $image, 0, 0, $x1, $y1, $CONF['user_avatar_width_big'], $CONF['user_avatar_width_big'], $x2-$x1, $y2-$y1);
	imagepng($thumbb,$file."-big");
	imagecopyresampled($thumbm, $image, 0, 0, $x1, $y1, $CONF['user_avatar_width_med'], $CONF['user_avatar_width_med'], $x2-$x1, $y2-$y1);
	imagepng($thumbm,$file."-med");
	imagecopyresampled($thumbs, $image, 0, 0, $x1, $y1, $CONF['user_avatar_width_small'], $CONF['user_avatar_width_small'], $x2-$x1, $y2-$y1);
	imagepng($thumbs,$file."-small");

	$user->setAvatarFile($file);
	$result = $user->save();

	unlink($file);
	unlink($file."-big");
	unlink($file."-med");
	unlink($file."-small");

	if ($result=='ok') return array('ok'=>true, 'error'=>'');
	else return array('ok'=>false, 'error'=>"$result");
}
Example #14
0
 public function getImage($scale = NULL)
 {
     if (!$scale) {
         $scale = max(1, floor(600 / $this->width));
     }
     $img = imagecreatetruecolor($this->width * $scale, $this->height * $scale);
     $bg = imagecolorallocate($img, 0, 0, 0);
     for ($i = 0; $i < $this->width; $i++) {
         for ($j = 0; $j < $this->height; $j++) {
             list($r, $g, $b) = $this->grid[$i][$j];
             $color = imagecolorexact($img, $r, $g, $b);
             if ($color == -1) {
                 $color = imagecolorallocate($img, $r, $g, $b);
             }
             $x = $i * $scale;
             $y = $j * $scale;
             imagefilledrectangle($img, $x, $y, $x + $scale, $y + $scale, $color);
         }
     }
     return $img;
 }
Example #15
0
 public static function cap_show()
 {
     $mainDir = SettingsManager::getInstance()->getMainDir();
     if (!file_exists($mainDir . '/tmp')) {
         mkdir($mainDir . '/tmp');
     }
     $filename = 'cap_' . rand(44, 888888) . '.png';
     $op[0] = '+';
     $op[1] = '-';
     $cap_r1 = rand(0, 99);
     $cap_op = $op[rand(0, 1)];
     $cap_r2 = rand(0, 22);
     eval('$cap_result = ' . $cap_r1 . $cap_op . $cap_r2 . ';');
     $_SESSION['captcha_result'] = md5($cap_result);
     $img = imagecreatetruecolor(50, 30);
     imagettftext($img, 12, rand(-30, 30), 2, 20, imagecolorexact($img, rand(150, 255), rand(150, 255), rand(150, 255)), $mainDir . '/resources/arial.ttf', $cap_r1);
     imagettftext($img, 12, rand(-15, 15), 20, 20, imagecolorexact($img, rand(150, 255), rand(150, 255), rand(150, 255)), $mainDir . '/resources/arial.ttf', $cap_op);
     imagettftext($img, 12, rand(-35, 35), 30, 20, imagecolorexact($img, rand(150, 255), rand(150, 255), rand(150, 255)), $mainDir . '/resources/arial.ttf', $cap_r2);
     imagepng($img, $mainDir . '/tmp/' . $filename);
     imagedestroy($img);
     echo '<img src="' . SettingsManager::getInstance()->getMainUrl() . '/tmp/' . $filename . '" alt=""/>';
     self::cap_cleanup();
 }
Example #16
0
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 -->
<?php 
list($width, $height) = getimagesize('./marker.png');
$img = imagecreatetruecolor($width, $height);
$srcimg = imagecreatefrompng('./marker.png');
$white = imagecolorexact($img, 255, 255, 255);
imagefilledrectangle($img, 0, 0, 20, 34, $white);
imagecopyresampled($img, $srcimg, 0, 0, 0, 0, $width, $height, $width, $height);
imagecolortransparent($img, $white);
if (isset($_REQUEST['color'])) {
    $cols = $_REQUEST['color'];
    $cols = explode(",", $_REQUEST['color']);
    $color = imagecolorallocate($img, $cols[0], $cols[1], $cols[2]);
} else {
    $color = imagecolorallocate($img, 0, 101, 255);
}
imagefill($img, 5, 5, $color);
header("Content-type: image/png");
imagepng($img);
imagecolordeallocate($color);
imagedestroy($srcimg);
Example #17
0
 public function getImage($scale = NULL)
 {
     if (!$scale) {
         $scale = max(1, floor(600 / $this->som->width));
     }
     $img = imagecreate($this->som->width * $scale, $this->som->height * $scale);
     $bg = imagecolorallocate($img, 0, 0, 0);
     for ($i = 0; $i < $this->som->width; $i++) {
         for ($j = 0; $j < $this->som->height; $j++) {
             $r = 255 * $this->map[$i][$j];
             $g = intval($r / 3);
             $b = 255 * ((1 - $this->map[$i][$j]) / 2);
             $color = imagecolorexact($img, $r, $g, $b);
             if ($color == -1) {
                 $color = imagecolorallocate($img, $r, $g, $b);
             }
             $x = $i * $scale;
             $y = $j * $scale;
             imagefilledrectangle($img, $x, $y, $x + $scale, $y + $scale, $color);
         }
     }
     $green = imagecolorexact($img, 0, 255, 0);
     if ($green == -1) {
         $green = imagecolorallocate($img, 0, 255, 0);
     }
     $x = $this->max_x * $scale;
     $y = $this->max_y * $scale;
     imagefilledrectangle($img, $x, $y, $x + $scale, $y + $scale, $green);
     $yellow = imagecolorexact($img, 255, 255, 0);
     if ($yellow == -1) {
         $yellow = imagecolorallocate($img, 255, 255, 0);
     }
     $x = $this->min_x * $scale;
     $y = $this->min_y * $scale;
     imagefilledrectangle($img, $x, $y, $x + $scale, $y + $scale, $yellow);
     return $img;
 }
Example #18
0
 function imagecreatefrombmp($filename)
 {
     global $gd2;
     $fp = fopen($filename, 'rb');
     $errors = error_reporting(0);
     $header = unpack('vtype/Vsize/Vreserved/Voffset', fread($fp, 14));
     $info = unpack('Vsize/Vwidth/Vheight/vplanes/vbits/Vcompression/Vimagesize/Vxres/Vyres/Vncolor/Vcolorimportant', fread($fp, 40));
     if ($header['type'] != 0x4d42) {
         false;
     }
     if ($gd2) {
         $dst_img = imagecreatetruecolor($info['width'], $info['height']);
     } else {
         $dst_img = imagecreate($info['width'], $info['height']);
     }
     $palette_size = $header['offset'] - 54;
     $info['ncolor'] = $palette_size / 4;
     $palette = array();
     $palettedata = fread($fp, $palette_size);
     $n = 0;
     for ($j = 0; $j < $palette_size; $j++) {
         $b = ord($palettedata[$j++]);
         $g = ord($palettedata[$j++]);
         $r = ord($palettedata[$j++]);
         $palette[$n++] = imagecolorallocate($dst_img, $r, $g, $b);
     }
     $scan_line_size = $info['bits'] * $info['width'] + 7 >> 3;
     $scan_line_align = $scan_line_size & 3 ? 4 - ($scan_line_size & 3) : 0;
     for ($y = 0, $l = $info['height'] - 1; $y < $info['height']; $y++, $l--) {
         fseek($fp, $header['offset'] + ($scan_line_size + $scan_line_align) * $l);
         $scan_line = fread($fp, $scan_line_size);
         if (strlen($scan_line) < $scan_line_size) {
             continue;
         }
         if ($info['bits'] == 32) {
             $x = 0;
             for ($j = 0; $j < $scan_line_size; $x++) {
                 $b = ord($scan_line[$j++]);
                 $g = ord($scan_line[$j++]);
                 $r = ord($scan_line[$j++]);
                 $j++;
                 $color = imagecolorexact($dst_img, $r, $g, $b);
                 if ($color == -1) {
                     $color = imagecolorallocate($dst_img, $r, $g, $b);
                     // Gah!  Out of colors?  Stupid GD 1... try anyhow.
                     if ($color == -1) {
                         $color = imagecolorclosest($dst_img, $r, $g, $b);
                     }
                 }
                 imagesetpixel($dst_img, $x, $y, $color);
             }
         } elseif ($info['bits'] == 24) {
             $x = 0;
             for ($j = 0; $j < $scan_line_size; $x++) {
                 $b = ord($scan_line[$j++]);
                 $g = ord($scan_line[$j++]);
                 $r = ord($scan_line[$j++]);
                 $color = imagecolorexact($dst_img, $r, $g, $b);
                 if ($color == -1) {
                     $color = imagecolorallocate($dst_img, $r, $g, $b);
                     // Gah!  Out of colors?  Stupid GD 1... try anyhow.
                     if ($color == -1) {
                         $color = imagecolorclosest($dst_img, $r, $g, $b);
                     }
                 }
                 imagesetpixel($dst_img, $x, $y, $color);
             }
         } elseif ($info['bits'] == 16) {
             $x = 0;
             for ($j = 0; $j < $scan_line_size; $x++) {
                 $b1 = ord($scan_line[$j++]);
                 $b2 = ord($scan_line[$j++]);
                 $word = $b2 * 256 + $b1;
                 $b = ($word & 31) * 255 / 31;
                 $g = ($word >> 5 & 31) * 255 / 31;
                 $r = ($word >> 10 & 31) * 255 / 31;
                 // Scale the image colors up properly.
                 $color = imagecolorexact($dst_img, $r, $g, $b);
                 if ($color == -1) {
                     $color = imagecolorallocate($dst_img, $r, $g, $b);
                     // Gah!  Out of colors?  Stupid GD 1... try anyhow.
                     if ($color == -1) {
                         $color = imagecolorclosest($dst_img, $r, $g, $b);
                     }
                 }
                 imagesetpixel($dst_img, $x, $y, $color);
             }
         } elseif ($info['bits'] == 8) {
             $x = 0;
             for ($j = 0; $j < $scan_line_size; $x++) {
                 imagesetpixel($dst_img, $x, $y, $palette[ord($scan_line[$j++])]);
             }
         } elseif ($info['bits'] == 4) {
             $x = 0;
             for ($j = 0; $j < $scan_line_size; $x++) {
                 $byte = ord($scan_line[$j++]);
                 imagesetpixel($dst_img, $x, $y, $palette[(int) ($byte / 16)]);
                 if (++$x < $info['width']) {
                     imagesetpixel($dst_img, $x, $y, $palette[$byte & 15]);
                 }
             }
         } else {
             // Sorry, I'm just not going to do monochrome :P.
         }
     }
     fclose($fp);
     error_reporting($errors);
     return $dst_img;
 }
Example #19
0
 function Allocate($color)
 {
     list($r, $g, $b) = $this->color($color);
     $index = imagecolorexact($this->img, $r, $g, $b);
     if ($index == -1) {
         $index = imagecolorallocate($this->img, $r, $g, $b);
         if (USE_APPROX_COLORS && $index == -1) {
             $index = imagecolorresolve($this->img, $r, $g, $b);
         }
     }
     return $index;
 }
Example #20
0
 private function testColorExists($colorArray)
 {
     $r = $colorArray['r'];
     $g = $colorArray['g'];
     $b = $colorArray['b'];
     if (imagecolorexact($this->imageResized, $r, $g, $b) == -1) {
         return false;
     } else {
         return true;
     }
 }
Example #21
0
 /**
  * Allocates a color
  *
  * This function tries to allocate the requested color. If the color 
  * already exists in the imaga it will be reused.
  * 
  * @param ezcGraphColor $color 
  * @return int Color index
  */
 protected function allocate(ezcGraphColor $color)
 {
     $image = $this->getImage();
     if ($color->alpha > 0) {
         $fetched = imagecolorexactalpha($image, $color->red, $color->green, $color->blue, $color->alpha / 2);
         if ($fetched < 0) {
             $fetched = imagecolorallocatealpha($image, $color->red, $color->green, $color->blue, $color->alpha / 2);
         }
         return $fetched;
     } else {
         $fetched = imagecolorexact($image, $color->red, $color->green, $color->blue);
         if ($fetched < 0) {
             $fetched = imagecolorallocate($image, $color->red, $color->green, $color->blue);
         }
         return $fetched;
     }
 }
 /**
  * Draw axis
  *
  * Draw both x and y axis to the plot.
  *
  *@access private
  *@uses $MinX
  *@uses $MaxX
  *@uses $MinY
  *@uses $MaxY
  *@uses GetImageX()
  *@uses GetImageY()
  *@param ImageResource &$ImageResource ImageResource representation of the plot.
  */
 function DrawAxis(&$ImageResource)
 {
     $Black = imagecolorexact($ImageResource, 0, 0, 0);
     //Draw X-axis
     imageline($ImageResource, $this->GetImageX(0), $this->GetImageY($this->MinY), $this->GetImageX(0), $this->GetImageY($this->MaxY), $Black);
     //Draw Y-axis
     imageline($ImageResource, $this->GetImageX($this->MinX), $this->GetImageY(0), $this->GetImageX($this->MaxX), $this->GetImageY(0), $Black);
 }
Example #23
0
 /**
  * Allocate a color for an image.
  *
  * @param int      $red   Value of red component (between 0 and 255)
  * @param int      $green Value of green component (between 0 and 255)
  * @param int      $blue  Value of blue component (between 0 and 255)
  * @param int|null $alpha Optional value of alpha component (between 0 and 127). 0 = opaque, 127 = transparent.
  *
  * @return Color
  */
 public function allocateColor($red, $green, $blue, $alpha = null)
 {
     // Verify parameters before trying to allocate
     new Color($red, $green, $blue, $alpha);
     // Reuse same color if its already in index
     if ($alpha === null) {
         $index = imagecolorexact($this->resource, $red, $green, $blue);
     } else {
         $index = imagecolorexactalpha($this->resource, $red, $green, $blue, $alpha);
     }
     if ($index !== -1) {
         return new Color($red, $green, $blue, $alpha, $index);
     }
     // Allocate new color
     if ($alpha === null) {
         $index = imagecolorallocate($this->resource, $red, $green, $blue);
     } else {
         $index = imagecolorallocatealpha($this->resource, $red, $green, $blue, $alpha);
     }
     if ($index === false) {
         throw new InvalidArgumentException('Failed to create color');
     }
     return new Color($red, $green, $blue, $alpha, $index);
 }
Example #24
0
/**
 * Reads image from a BMP file and converts it to image resource
 * @global int $jpexs_CurrentBit Internal variable
 * @param string $file File to read BMP image from
 * @return resource Image resource or false on error
 *
 * Note:
 *  Reading RLE compressed bitmaps is EXPERIMENTAL
 *  Reading palette based bitmaps with less than 8bit palette is EXPERIMENTAL
 */
function imagecreatefrombmp($file)
{
    global $jpexs_CurrentBit;
    $f = fopen($file, "r");
    $Header = fread($f, 2);
    if ($Header == "BM") {
        $Size = jpexs_freaddword($f);
        $Reserved1 = jpexs_freadword($f);
        $Reserved2 = jpexs_freadword($f);
        $FirstByteOfImage = jpexs_freaddword($f);
        $SizeBITMAPINFOHEADER = jpexs_freaddword($f);
        $Width = jpexs_freaddword($f);
        $Height = jpexs_freaddword($f);
        $biPlanes = jpexs_freadword($f);
        $biBitCount = jpexs_freadword($f);
        $RLECompression = jpexs_freaddword($f);
        $WidthxHeight = jpexs_freaddword($f);
        $biXPelsPerMeter = jpexs_freaddword($f);
        $biYPelsPerMeter = jpexs_freaddword($f);
        $NumberOfPalettesUsed = jpexs_freaddword($f);
        $NumberOfImportantColors = jpexs_freaddword($f);
        if ($biBitCount < 24) {
            $img = imagecreate($Width, $Height);
            $Colors = pow(2, $biBitCount);
            for ($p = 0; $p < $Colors; $p++) {
                $B = jpexs_freadbyte($f);
                $G = jpexs_freadbyte($f);
                $R = jpexs_freadbyte($f);
                $Reserved = jpexs_freadbyte($f);
                $Palette[] = imagecolorallocate($img, $R, $G, $B);
            }
            if ($RLECompression == 0) {
                $Zbytek = (4 - ceil($Width / (8 / $biBitCount)) % 4) % 4;
                for ($y = $Height - 1; $y >= 0; $y--) {
                    $jpexs_CurrentBit = 0;
                    for ($x = 0; $x < $Width; $x++) {
                        $C = jpexs_freadbits($f, $biBitCount);
                        imagesetpixel($img, $x, $y, $Palette[$C]);
                    }
                    if ($jpexs_CurrentBit != 0) {
                        jpexs_freadbyte($f);
                    }
                    for ($g = 0; $g < $Zbytek; $g++) {
                        jpexs_freadbyte($f);
                    }
                }
            }
        }
        if ($RLECompression == 1) {
            $y = $Height;
            $pocetb = 0;
            while (true) {
                $y--;
                $prefix = jpexs_freadbyte($f);
                $suffix = jpexs_freadbyte($f);
                $pocetb += 2;
                $echoit = false;
                if ($echoit) {
                    echo "Prefix: {$prefix} Suffix: {$suffix}<BR>";
                }
                if ($prefix == 0 and $suffix == 1) {
                    break;
                }
                if (feof($f)) {
                    break;
                }
                while (!($prefix == 0 and $suffix == 0)) {
                    if ($prefix == 0) {
                        $pocet = $suffix;
                        $Data .= fread($f, $pocet);
                        $pocetb += $pocet;
                        if ($pocetb % 2 == 1) {
                            jpexs_freadbyte($f);
                            $pocetb++;
                        }
                    }
                    if ($prefix > 0) {
                        $pocet = $prefix;
                        for ($r = 0; $r < $pocet; $r++) {
                            $Data .= chr($suffix);
                        }
                    }
                    $prefix = jpexs_freadbyte($f);
                    $suffix = jpexs_freadbyte($f);
                    $pocetb += 2;
                    if ($echoit) {
                        echo "Prefix: {$prefix} Suffix: {$suffix}<BR>";
                    }
                }
                for ($x = 0; $x < strlen($Data); $x++) {
                    imagesetpixel($img, $x, $y, $Palette[ord($Data[$x])]);
                }
                $Data = "";
            }
        }
        if ($RLECompression == 2) {
            $y = $Height;
            $pocetb = 0;
            /*while(!feof($f))
            	 echo freadbyte($f)."_".freadbyte($f)."<BR>";*/
            while (true) {
                //break;
                $y--;
                $prefix = jpexs_freadbyte($f);
                $suffix = jpexs_freadbyte($f);
                $pocetb += 2;
                $echoit = false;
                if ($echoit) {
                    echo "Prefix: {$prefix} Suffix: {$suffix}<BR>";
                }
                if ($prefix == 0 and $suffix == 1) {
                    break;
                }
                if (feof($f)) {
                    break;
                }
                while (!($prefix == 0 and $suffix == 0)) {
                    if ($prefix == 0) {
                        $pocet = $suffix;
                        $jpexs_CurrentBit = 0;
                        for ($h = 0; $h < $pocet; $h++) {
                            $Data .= chr(jpexs_freadbits($f, 4));
                        }
                        if ($jpexs_CurrentBit != 0) {
                            jpexs_freadbits($f, 4);
                        }
                        $pocetb += ceil($pocet / 2);
                        if ($pocetb % 2 == 1) {
                            jpexs_freadbyte($f);
                            $pocetb++;
                        }
                    }
                    if ($prefix > 0) {
                        $pocet = $prefix;
                        $i = 0;
                        for ($r = 0; $r < $pocet; $r++) {
                            if ($i % 2 == 0) {
                                $Data .= chr($suffix % 16);
                            } else {
                                $Data .= chr(floor($suffix / 16));
                            }
                            $i++;
                        }
                    }
                    $prefix = jpexs_freadbyte($f);
                    $suffix = jpexs_freadbyte($f);
                    $pocetb += 2;
                    if ($echoit) {
                        echo "Prefix: {$prefix} Suffix: {$suffix}<BR>";
                    }
                }
                for ($x = 0; $x < strlen($Data); $x++) {
                    imagesetpixel($img, $x, $y, $Palette[ord($Data[$x])]);
                }
                $Data = "";
            }
        }
        if ($biBitCount == 24) {
            $img = imagecreatetruecolor($Width, $Height);
            $Zbytek = $Width % 4;
            for ($y = $Height - 1; $y >= 0; $y--) {
                for ($x = 0; $x < $Width; $x++) {
                    $B = jpexs_freadbyte($f);
                    $G = jpexs_freadbyte($f);
                    $R = jpexs_freadbyte($f);
                    $color = imagecolorexact($img, $R, $G, $B);
                    if ($color == -1) {
                        $color = imagecolorallocate($img, $R, $G, $B);
                    }
                    imagesetpixel($img, $x, $y, $color);
                }
                for ($z = 0; $z < $Zbytek; $z++) {
                    jpexs_freadbyte($f);
                }
            }
        }
        return $img;
    } else {
        return false;
    }
    fclose($f);
}
Example #25
0
 /**
  * Create a color resource using an RGB array
  *
  * @param array $color array(1,2,3) of RGB calues
  * @param array[optional] $image The image to allocate for (map is default)
  *
  * @return GD color resource
  */
 protected function color(array $color, $image = false)
 {
     if (!is_array($image)) {
         $image = $this->map;
     }
     list($r, $g, $b) = $color;
     // Try to allocate the color from the palette ..
     $colorRes = imagecolorexact($image['image'], $r, $g, $b);
     if ($colorRes == -1) {
         // .. if the color doesn't exist within the palette add it to the palette (which can hit a limit if the palette gets full) ..
         $colorRes = imageColorAllocate($image['image'], $r, $g, $b);
         if (!$colorRes) {
             // .. and failing that get the best available thing.
             $colorRes = imageColorClosest($image['image'], $r, $g, $b);
         }
     }
     return $colorRes;
 }
 function convertPngImage($fName)
 {
     $fOutname = $fName . ".tmp";
     $img = imagecreatefrompng($fName);
     $width = imagesx($img);
     $height = imagesy($img);
     $img2 = imagecreatetruecolor($width, $height);
     imagefill($img2, 0, 0, imagecolorexact($img2, 255, 255, 255));
     imagecopyresampled($img2, $img, 0, 0, 0, 0, $width, $height, $width, $height);
     imagepng($img2, $fOutname);
     return $fOutname;
 }
Example #27
0
 /**
  * @param       int             red 0-255
  * @param       int             green 0-255
  * @param       int             blue 0-255
  * @return      bool
  */
 public function hasColor($red, $green, $blue)
 {
     // TODO issue with gif and limited color rang
     if (!is_null($this->imageResource)) {
         $colorIndex = imagecolorexact($this->imageResource, (int) $red, (int) $green, (int) $blue);
         if ($colorIndex >= 0) {
             return true;
         }
     }
     return false;
 }
Example #28
0
 /**
  * Returns the color index that exactly matches the given RGB value. Uses
  * PHP's imagecolorexact()
  *
  * @param mixed $R Red or RGBA array
  */
 function getExactColor($R, $G = null, $B = null)
 {
     if (is_array($R)) {
         return imagecolorexact($this->handle, $R['red'], $R['green'], $R['blue']);
     } else {
         return imagecolorexact($this->handle, $R, $G, $B);
     }
 }
Example #29
0
 function Allocate($aColor, $aAlpha = 0.0)
 {
     list($r, $g, $b, $a) = $this->color($aColor);
     // If alpha is specified in the color string then this
     // takes precedence over the second argument
     if ($a > 0) {
         $aAlpha = $a;
     }
     if (@$GLOBALS['gd2'] == true) {
         if ($aAlpha < 0 || $aAlpha > 1) {
             JpGraphError::Raise('Alpha parameter for color must be between 0.0 and 1.0');
             exit(1);
         }
         return imagecolorresolvealpha($this->img, $r, $g, $b, round($aAlpha * 127));
     } else {
         $index = imagecolorexact($this->img, $r, $g, $b);
         if ($index == -1) {
             $index = imagecolorallocate($this->img, $r, $g, $b);
             if (USE_APPROX_COLORS && $index == -1) {
                 $index = imagecolorresolve($this->img, $r, $g, $b);
             }
         }
         return $index;
     }
 }
Example #30
0
 public function execute($inputImagePath)
 {
     // 前処理
     $filestat = @stat($inputImagePath);
     $imagesize = getimagesize($inputImagePath);
     $inputImageWidth = $imagesize[0];
     $inputImageHeight = $imagesize[1];
     $inputImageType = $imagesize[2];
     // 今回実行用の一時変数をセット
     $output_image_width = $this->outputImageWidth;
     $output_image_height = is_null($this->outputImageHeight) ? $inputImageHeight * ($output_image_width / $inputImageWidth) : $this->outputImageHeight;
     // GIF 画像は縮小後も GIF 画像で出力する。旧機種対応などで、機種用の画像形式に変換したい場合、expr3 に固定する。
     $output_image_type = $inputImageType == IMAGETYPE_GIF ? 'gif' : $this->outputImageType;
     $outputImageName = sha1($inputImagePath . '_' . $this->outputImageWidth . '_' . $this->outputFileSize . '_' . $filestat['mtime']) . '.' . $output_image_type;
     $outputImagePath = $this->outputImageDir . '/' . $outputImageName;
     if ($inputImageWidth <= $output_image_width) {
         if ($inputImageHeight <= $output_image_height) {
             $output_image_width = $inputImageWidth;
             $output_image_height = $inputImageHeight;
         } else {
             $output_image_width = $inputImageWidth * ($output_image_height / $inputImageHeight);
         }
     } else {
         if ($inputImageHeight <= $output_image_height) {
             $output_image_height = $inputImageHeight * ($output_image_width / $inputImageWidth);
         } else {
             if ($output_image_width / $inputImageWidth < $output_image_height / $inputImageHeight) {
                 $output_image_height = $inputImageHeight * ($output_image_width / $inputImageWidth);
             } else {
                 $output_image_width = $inputImageWidth * ($output_image_height / $inputImageHeight);
             }
         }
     }
     // ファイルが存在するか確認し、存在しない場合のみ作成する
     if (file_exists($outputImagePath)) {
         $info['convert'] = FALSE;
     } else {
         // 元ファイル作成
         switch ($inputImageType) {
             case IMAGETYPE_GIF:
                 $tempImage = imagecreatefromgif($inputImagePath);
                 $arrTransparentColor = $this->getTransparentColor($tempImage);
                 if (!empty($arrTransparentColor)) {
                     imagecolortransparent($tempImage, $arrTransparentColor);
                 }
                 break;
             case IMAGETYPE_JPEG:
                 $tempImage = imagecreatefromjpeg($inputImagePath);
                 break;
             case IMAGETYPE_PNG:
                 $tempImage = imagecreatefrompng($inputImagePath);
                 break;
             case IMAGETYPE_WBMP:
                 $tempImage = imagecreatefromwbmp($inputImagePath);
                 break;
         }
         if (!$tempImage) {
             return false;
         }
         $scale = 1.0;
         $outputImagePathTemp = $outputImagePath . '.tmp-' . rand();
         do {
             // 空ファイル作成
             if ($output_image_type == 'gif') {
                 // 縮小時のノイズ防止のためインデックスカラーで処理する。特に透過色を扱う上で重要。
                 $outputImage = ImageCreate($output_image_width * $scale, $output_image_height * $scale);
             } else {
                 $outputImage = ImageCreateTruecolor($output_image_width * $scale, $output_image_height * $scale);
             }
             ImageCopyResampled($outputImage, $tempImage, 0, 0, 0, 0, $output_image_width * $scale, $output_image_height * $scale, $inputImageWidth, $inputImageHeight);
             // ファイル出力
             @unlink($outputImagePathTemp);
             switch ($output_image_type) {
                 case 'gif':
                     if (!empty($arrTransparentColor)) {
                         $transparent_color_id = imagecolorexact($outputImage, $arrTransparentColor['red'], $arrTransparentColor['green'], $arrTransparentColor['blue']);
                         imagecolortransparent($outputImage, $transparent_color_id);
                     }
                     imagegif($outputImage, $outputImagePathTemp);
                     break;
                 case 'jpg':
                     $quality = 75;
                     // 表示可能なファイルサイズ以下になるまで、10%ずつクオリティを調整する
                     do {
                         @unlink($outputImagePathTemp);
                         imagejpeg($outputImage, $outputImagePathTemp, $quality);
                         $quality -= 10;
                         clearstatcache();
                     } while (filesize($outputImagePathTemp) > $this->outputFileSize && $quality > 0);
                     break;
                 case 'png':
                     imagepng($outputImage, $outputImagePathTemp);
                     break;
                 case 'bmp':
                     imagewbmp($outputImage, $outputImagePathTemp);
                     break;
                 default:
                     GC_Utils_Ex::gfPrintLog('不正な画像タイプ: ');
                     break;
             }
             // メモリ開放
             imagedestroy($outputImage);
             $scale -= 0.1;
             clearstatcache();
         } while (filesize($outputImagePathTemp) > $this->outputFileSize && $scale >= 0.5);
         rename($outputImagePathTemp, $outputImagePath);
         // メモリ開放
         imagedestroy($tempImage);
         $info['convert'] = TRUE;
     }
     $info['outputImagePath'] = $outputImagePath;
     $info['outputImageName'] = $outputImageName;
     return $info;
 }