/** * {@inheritdoc} */ public function analyze($filename) { $imageSize = @getimagesize($filename); if ($imageSize === false) { throw new UnsupportedFileException('File type not supported.'); } $imageInfo = new ImageInfo(); $type = null; $colors = null; if ($imageSize[2] === IMAGETYPE_JPEG) { $gd = imagecreatefromjpeg($filename); $type = imageistruecolor($gd) ? 'TRUECOLOR' : 'PALETTE'; $colors = imagecolorstotal($gd); imagedestroy($gd); } elseif ($imageSize[2] === IMAGETYPE_GIF) { $gd = imagecreatefromgif($filename); $type = imageistruecolor($gd) ? 'TRUECOLOR' : 'PALETTE'; $colors = imagecolorstotal($gd); imagedestroy($gd); } elseif ($imageSize[2] === IMAGETYPE_PNG) { $gd = imagecreatefrompng($filename); $type = imageistruecolor($gd) ? 'TRUECOLOR' : 'PALETTE'; $colors = imagecolorstotal($gd); imagedestroy($gd); } $imageInfo->setAnalyzer(get_class($this))->setSize($imageSize[0], $imageSize[1])->setResolution(null, null)->setUnits(null)->setFormat($this->mapFormat($imageSize[2]))->setColors($colors)->setType($type)->setColorspace(!empty($imageSize['channels']) ? $imageSize['channels'] === 4 ? 'CMYK' : 'RGB' : 'RGB')->setDepth($imageSize['bits'])->setCompression(null)->setQuality(null)->setProfiles(null); return $imageInfo; }
public function resize($maxW = null, $maxH = null, $canEnlarge = false) { $src = $this->_rawImage; if ($maxW === null && $maxH === null) { return $src; } if (imageistruecolor($src)) { imageAlphaBlending($src, true); imageSaveAlpha($src, true); } $srcW = imagesx($src); $srcH = imagesy($src); $width = $maxW && ($canEnlarge || $maxW <= $srcW) ? $maxW : $srcW; $height = $maxH && ($canEnlarge || $maxH <= $srcW) ? $maxH : $srcH; $ratio_orig = $srcW / $srcH; if ($width / $height > $ratio_orig) { $width = $height * $ratio_orig; } else { $height = $width / $ratio_orig; } $maxW = $maxW ? $maxW : $width; $maxH = $maxH ? $maxH : $height; $img = imagecreatetruecolor($maxW, $maxH); $trans_colour = imagecolorallocatealpha($img, 0, 0, 0, 127); imagefill($img, 0, 0, $trans_colour); $offsetX = ($maxW - $width) / 2; $offsetY = ($maxH - $height) / 2; imagecopyresampled($img, $src, $offsetX, $offsetY, 0, 0, $width, $height, $srcW, $srcH); imagealphablending($img, true); imagesavealpha($img, true); return $img; }
/** * Convert an image. * * @param img.Image image * @return bool * @throws img.ImagingException */ public function convert($image) { // Create temporary variable as local variable access is faster // than member variable access. $handle = $image->handle; if (imageistruecolor($handle)) { $l = []; $h = $image->getHeight(); $w = $image->getWidth(); for ($y = 0; $y < $h; $y++) { for ($x = 0; $x < $w; $x++) { $rgb = imagecolorat($handle, $x, $y); if (!isset($l[$rgb])) { $g = 0.299 * ($rgb >> 16 & 0xff) + 0.587 * ($rgb >> 8 & 0xff) + 0.114 * ($rgb & 0xff); $l[$rgb] = imagecolorallocate($handle, $g, $g, $g); } imagesetpixel($handle, $x, $y, $l[$rgb]); } } unset($l); } else { for ($i = 0, $t = imagecolorstotal($handle); $i < $t; $i++) { $c = imagecolorsforindex($handle, $i); $g = 0.299 * $c['red'] + 0.587 * $c['green'] + 0.114 * $c['blue']; imagecolorset($handle, $i, $g, $g, $g); } } }
/** * Fit small image to specified bound * * @param string $src * @param string $dest * @param int $width * @param int $height * @return bool */ public function fit($src, $dest, $width, $height) { // Calculate $size = getimagesize($src); $ratio = max($width / $size[0], $height / $size[1]); $old_width = $size[0]; $old_height = $size[1]; $new_width = intval($old_width * $ratio); $new_height = intval($old_height * $ratio); // Resize @ini_set('memory_limit', apply_filters('image_memory_limit', WP_MAX_MEMORY_LIMIT)); $image = imagecreatefromstring(file_get_contents($src)); $new_image = wp_imagecreatetruecolor($new_width, $new_height); imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height); if (IMAGETYPE_PNG == $size[2] && function_exists('imageistruecolor') && !imageistruecolor($image)) { imagetruecolortopalette($new_image, false, imagecolorstotal($image)); } // Destroy old image imagedestroy($image); // Save switch ($size[2]) { case IMAGETYPE_GIF: $result = imagegif($new_image, $dest); break; case IMAGETYPE_PNG: $result = imagepng($new_image, $dest); break; default: $result = imagejpeg($new_image, $dest); break; } imagedestroy($new_image); return $result; }
/** * Convert an image. Returns TRUE when successfull, FALSE if image is * not a truecolor image. * * @param img.Image image * @return bool * @throws img.ImagingException */ public function convert($image) { if (!imageistruecolor($image->handle)) { return FALSE; } return imagetruecolortopalette($image->handle, $this->dither, $this->ncolors); }
public function testGdResourceToTruecolor() { $resource = imagecreate(10, 10); $this->assertFalse(imageistruecolor($resource)); Helper::gdResourceToTruecolor($resource); $this->assertTrue(imageistruecolor($resource)); }
/** * Output an image. If the image is true-color, it will be converted * to a paletted image first using imagetruecolortopalette(). * * @param resource handle * @return bool */ public function output($handle) { if (imageistruecolor($handle)) { imagetruecolortopalette($handle, $this->dither, $this->ncolors); } return imagegif($handle); }
/** * {@inheritdoc} */ protected function load() { // Return immediately if the image file is not valid. if (!$this->isValid()) { return FALSE; } switch ($this->getType()) { case GDToolkitWebP::IMAGETYPE_WEBP: $function = 'imagecreatefromwebp'; break; default: $function = 'imagecreatefrom' . image_type_to_extension($this->getType(), FALSE); } if (function_exists($function) && ($resource = $function($this->getImage()->getSource()))) { $this->setResource($resource); if (imageistruecolor($resource)) { return TRUE; } else { // Convert indexed images to true color, so that filters work // correctly and don't result in unnecessary dither. $new_image = $this->createTmp($this->getType(), imagesx($resource), imagesy($resource)); if ($ret = (bool) $new_image) { imagecopy($new_image, $resource, 0, 0, 0, 0, imagesx($resource), imagesy($resource)); imagedestroy($resource); $this->setResource($new_image); } return $ret; } } return FALSE; }
function imageflip(&$image, $x = 0, $y = 0, $width = null, $height = null) { if ($width < 1) { $width = imagesx($image); } if ($height < 1) { $height = imagesy($image); } // Truecolor provides better results, if possible. if (function_exists('imageistruecolor') && imageistruecolor($image)) { $tmp = imagecreatetruecolor(1, $height); } else { $tmp = imagecreate(1, $height); } $x2 = $x + $width - 1; for ($i = (int) floor(($width - 1) / 2); $i >= 0; $i--) { // Backup right stripe. imagecopy($tmp, $image, 0, 0, $x2 - $i, $y, 1, $height); // Copy left stripe to the right. imagecopy($image, $image, $x2 - $i, $y, $x + $i, $y, 1, $height); // Copy backuped right stripe to the left. imagecopy($image, $tmp, $x + $i, $y, 0, 0, 1, $height); } imagedestroy($tmp); return true; }
public function testConstructMethodAlwaysConvertResourceTrueColor() { $resource = imagecreatefromgif(__DIR__ . '/../../../data/lisbon1.gif'); $this->assertFalse(imageistruecolor($resource)); $adapter = new GdAdapter($resource); $this->assertTrue(imageistruecolor($adapter->getResource())); }
function get_hexcolor($im, $c) { if (imageistruecolor($im)) { return $c; } $colors = imagecolorsforindex($im, $c); return ($colors['red'] << 16) + ($colors['green'] << 8) + $colors['blue']; }
/** * Convert the image to 2 colours with dithering. */ protected function dither() { if (!imageistruecolor($this->image)) { imagepalettetotruecolor($this->image); } imagefilter($this->image, IMG_FILTER_GRAYSCALE); imagetruecolortopalette($this->image, true, 2); }
function ReadSourceFile($image_file_name) { $this->DestroyImage(); $this->ImageStatsSRC = @getimagesize($image_file_name); if ($this->ImageStatsSRC[2] == 3) { $this->ImageStatsSRC[2] = IMG_PNG; } $this->ImageMimeType = $this->ImageStatsSRC['mime']; $this->ImageTypeNo = $this->ImageStatsSRC[2]; switch ($this->ImageTypeNo) { case IMG_GIF: if (!(imagetypes() & $this->ImageTypeNo)) { return false; } $this->ImageTypeExt = '.gif'; $image_read_func = 'imagecreatefromgif'; break; case IMG_JPG: if (!(imagetypes() & $this->ImageTypeNo)) { return false; } if (function_exists('exif_read_data')) { $this->exif_get_data($image_file_name); } $this->ImageTypeExt = '.jpg'; $image_read_func = 'imagecreatefromjpeg'; break; case IMG_PNG: if (!(imagetypes() & $this->ImageTypeNo)) { return false; } $this->ImageTypeExt = '.png'; $image_read_func = 'imagecreatefrompng'; break; default: return false; } $this->ImageID = $image_read_func($image_file_name); if (function_exists('imageistruecolor')) { if (imageistruecolor($this->ImageID)) { if (function_exists('imageantialias')) { imageantialias($this->ImageID, true); } imagealphablending($this->ImageID, false); if (function_exists('imagesavealpha')) { $this->Alpha = true; imagesavealpha($this->ImageID, true); } } } $this->ChangeFlag = true; if ($this->ImageID) { return true; } else { return false; } }
/** * This function is almost equal to the image_resize (native function of wordpress) */ function image_resize($file, $max_w, $max_h, $crop = false, $far = false, $iar = false, $dest_path = null, $jpeg_quality = 90) { $image = wp_load_image($file); if (!is_resource($image)) { return new WP_Error('error_loading_image', $image); } $size = @getimagesize($file); if (!$size) { return new WP_Error('invalid_image', __('Could not read image size'), $file); } list($orig_w, $orig_h, $orig_type) = $size; $dims = mf_image_resize_dimensions($orig_w, $orig_h, $max_w, $max_h, $crop, $far, $iar); if (!$dims) { $dims = array(0, 0, 0, 0, $orig_w, $orig_h, $orig_w, $orig_h); } list($dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) = $dims; $newimage = imagecreatetruecolor($dst_w, $dst_h); imagealphablending($newimage, false); imagesavealpha($newimage, true); $transparent = imagecolorallocatealpha($newimage, 255, 255, 255, 127); imagefilledrectangle($newimage, 0, 0, $dst_w, $dst_h, $transparent); imagecopyresampled($newimage, $image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); // convert from full colors to index colors, like original PNG. if (IMAGETYPE_PNG == $orig_type && !imageistruecolor($image)) { imagetruecolortopalette($newimage, false, imagecolorstotal($image)); } // we don't need the original in memory anymore imagedestroy($image); $info = pathinfo($dest_path); $dir = $info['dirname']; $ext = $info['extension']; $name = basename($dest_path, ".{$ext}"); $destfilename = "{$dir}/{$name}.{$ext}"; if (IMAGETYPE_GIF == $orig_type) { if (!imagegif($newimage, $destfilename)) { return new WP_Error('resize_path_invalid', __('Resize path invalid')); } } elseif (IMAGETYPE_PNG == $orig_type) { if (!imagepng($newimage, $destfilename)) { return new WP_Error('resize_path_invalid', __('Resize path invalid')); } } else { // all other formats are converted to jpg //Todo: add option for use progresive JPG //imageinterlace($newimage, true); //Progressive JPG if (!imagejpeg($newimage, $destfilename, apply_filters('jpeg_quality', $jpeg_quality, 'image_resize'))) { return new WP_Error('resize_path_invalid', __('Resize path invalid')); } } imagedestroy($newimage); // Set correct file permissions $stat = stat(dirname($destfilename)); $perms = $stat['mode'] & 0666; //same permissions as parent folder, strip off the executable bits @chmod($destfilename, $perms); return $destfilename; }
protected function _save($image, $filename = null, $mime_type = null) { global $ewww_debug; if (!defined('EWWW_IMAGE_OPTIMIZER_DOMAIN')) { require_once plugin_dir_path(__FILE__) . 'ewww-image-optimizer.php'; } if (!defined('EWWW_IMAGE_OPTIMIZER_JPEGTRAN')) { ewww_image_optimizer_init(); } list($filename, $extension, $mime_type) = $this->get_output_format($filename, $mime_type); if (!$filename) { $filename = $this->generate_filename(null, null, $extension); } if ('image/gif' == $mime_type) { if (!$this->make_image($filename, 'imagegif', array($image, $filename))) { return new WP_Error('image_save_error', __('Image Editor Save Failed')); } } elseif ('image/png' == $mime_type) { // convert from full colors to index colors, like original PNG. if (function_exists('imageistruecolor') && !imageistruecolor($image)) { imagetruecolortopalette($image, false, imagecolorstotal($image)); } if (property_exists('WP_Image_Editor', 'quality')) { $compression_level = floor((101 - $this->quality) * 0.09); $ewww_debug .= "png quality = " . $this->quality . "<br>"; } else { $compression_level = floor((101 - false) * 0.09); } if (!$this->make_image($filename, 'imagepng', array($image, $filename, $compression_level))) { return new WP_Error('image_save_error', __('Image Editor Save Failed')); } } elseif ('image/jpeg' == $mime_type) { if (method_exists($this, 'get_quality')) { if (!$this->make_image($filename, 'imagejpeg', array($image, $filename, $this->get_quality()))) { return new WP_Error('image_save_error', __('Image Editor Save Failed')); } } else { if (!$this->make_image($filename, 'imagejpeg', array($image, $filename, apply_filters('jpeg_quality', $this->quality, 'image_resize')))) { return new WP_Error('image_save_error', __('Image Editor Save Failed')); } } } else { return new WP_Error('image_save_error', __('Image Editor Save Failed')); } // Set correct file permissions $stat = stat(dirname($filename)); $perms = $stat['mode'] & 0666; //same permissions as parent folder, strip off the executable bits @chmod($filename, $perms); ewww_image_optimizer_aux_images_loop($filename, true); $ewww_debug = "{$ewww_debug} image editor (gd) saved: {$filename} <br>"; $image_size = filesize($filename); $ewww_debug = "{$ewww_debug} image editor size: {$image_size} <br>"; ewww_image_optimizer_debug_log(); return array('path' => $filename, 'file' => wp_basename(apply_filters('image_make_intermediate_size', $filename)), 'width' => $this->size['width'], 'height' => $this->size['height'], 'mime-type' => $mime_type); }
function imagepalettetotruecolor(&$src) { if (imageistruecolor($src)) { return true; } $dst = imagecreatetruecolor(imagesx($src), imagesy($src)); imagecopy($dst, $src, 0, 0, 0, 0, imagesx($src), imagesy($src)); imagedestroy($src); $src = $dst; return true; }
function imagebmp($tipo = 'jpg', $imagesource = '', $imagebmp = 'new.bmp') { // Conviete imagen de JPG a BMP switch ($tipo) { case 'jpg': $im = imagecreatefromjpeg($imagesource); break; case 'png': $im = imagecreatefrompng($imagesource); break; case 'gif': $im = imagecreatefromgif($imagesource); break; } if (!$im) { return false; } $w = imagesx($im); $h = imagesy($im); $result = ''; if (!imageistruecolor($im)) { $tmp = imagecreatetruecolor($w, $h); imagecopy($tmp, $im, 0, 0, 0, 0, $w, $h); imagedestroy($im); $im =& $tmp; } $biBPLine = $w * 3; $biStride = $biBPLine + 3 & ~3; $biSizeImage = $biStride * $h; $bfOffBits = 54; $bfSize = $bfOffBits + $biSizeImage; $result .= substr('BM', 0, 2); $result .= pack('VvvV', $bfSize, 0, 0, $bfOffBits); $result .= pack('VVVvvVVVVVV', 40, $w, $h, 1, 24, 0, $biSizeImage, 0, 0, 0, 0); $numpad = $biStride - $biBPLine; for ($y = $h - 1; $y >= 0; --$y) { for ($x = 0; $x < $w; ++$x) { $col = imagecolorat($im, $x, $y); $result .= substr(pack('V', $col), 0, 3); } for ($i = 0; $i < $numpad; ++$i) { $result .= pack('C', 0); } } if ($imagebmp == "") { echo $result; } else { $file = fopen($imagebmp, "wb"); fwrite($file, $result); fclose($file); } return $imagebmp; }
/** * @param resource $resource * @param resource $controlResource * @return resource */ public function getOptimizedGdResource($resource, $controlResource) { if (imageistruecolor($resource)) { $resource = $this->rh->getPalettizedGdResource($resource); } $totalColors = imagecolorstotal($resource); $trans = imagecolortransparent($resource); $reds = new \SplFixedArray($totalColors); $greens = new \SplFixedArray($totalColors); $blues = new \SplFixedArray($totalColors); $i = 0; do { $colors = imagecolorsforindex($resource, $i); $reds[$i] = $colors['red']; $greens[$i] = $colors['green']; $blues[$i] = $colors['blue']; } while (++$i < $totalColors); if (imageistruecolor($controlResource)) { $controlResource = $this->rh->getPalettizedGdResource($controlResource); } $controlTotalColors = imagecolorstotal($controlResource); $controlTrans = imagecolortransparent($controlResource); $controlReds = new \SplFixedArray($controlTotalColors); $controlGreens = new \SplFixedArray($controlTotalColors); $controlBlues = new \SplFixedArray($controlTotalColors); $i = 0; do { $colors = imagecolorsforindex($controlResource, $i); $controlReds[$i] = $colors['red']; $controlGreens[$i] = $colors['green']; $controlBlues[$i] = $colors['blue']; } while (++$i < $controlTotalColors); $width = imagesx($resource); $height = imagesy($resource); $y = 0; do { $x = 0; do { $index = imagecolorat($resource, $x, $y); $red = $reds[$index]; $green = $greens[$index]; $blue = $blues[$index]; $controlIndex = imagecolorat($controlResource, $x, $y); $controlRed = $controlReds[$controlIndex]; $controlGreen = $controlGreens[$controlIndex]; $controlBlue = $controlBlues[$controlIndex]; if (($red & 0b11111100) === ($controlRed & 0b11111100) && ($green & 0b11111100) === ($controlGreen & 0b11111100) && ($blue & 0b11111100) === ($controlBlue & 0b11111100)) { imagesetpixel($resource, $x, $y, $trans); } } while (++$x !== $width); } while (++$y !== $height); return $resource; }
/** * Convert an image. Returns TRUE when successfull, FALSE if image is * not a truecolor image. * * @param img.Image image * @return bool * @throws img.ImagingException */ public function convert($image) { if (!imageistruecolor($image->handle)) { return false; } $tmp = Image::create($image->getWidth(), $image->getHeight(), IMG_TRUECOLOR); $tmp->copyFrom($image); imagetruecolortopalette($image->handle, $this->dither, $this->ncolors); imagecolormatch($tmp->handle, $image->handle); unset($tmp); return true; }
public function resize($width, $height, $keepRatio, $file, $target, $keepSmaller = true, $cropToFit = false, $jpegQuality = 75, $pngQuality = 6, $png8Bits = false) { list($oldWidth, $oldHeight, $type) = getimagesize($file); $i = getimagesize($file); switch ($type) { case IMAGETYPE_PNG: $source = imagecreatefrompng($file); break; case IMAGETYPE_JPEG: $source = imagecreatefromjpeg($file); break; case IMAGETYPE_GIF: $source = imagecreatefromgif($file); break; } $srcX = $srcY = 0; if ($cropToFit) { list($srcX, $srcY, $oldWidth, $oldHeight) = $this->_calculateSourceRectangle($oldWidth, $oldHeight, $width, $height); } elseif (!$keepSmaller || $oldWidth > $width || $oldHeight > $height) { if ($keepRatio) { list($width, $height) = $this->_calculateWidth($oldWidth, $oldHeight, $width, $height); } } else { $width = $oldWidth; $height = $oldHeight; } if (imageistruecolor($source) == false || $type == IMAGETYPE_GIF) { $thumb = imagecreate($width, $height); } else { $thumb = imagecreatetruecolor($width, $height); } imagealphablending($thumb, false); imagesavealpha($thumb, true); imagecopyresampled($thumb, $source, 0, 0, $srcX, $srcY, $width, $height, $oldWidth, $oldHeight); if ($type == IMAGETYPE_PNG && $png8Bits === true) { imagetruecolortopalette($thumb, true, 255); } switch ($type) { case IMAGETYPE_PNG: imagepng($thumb, $target, $pngQuality); break; case IMAGETYPE_JPEG: imagejpeg($thumb, $target, $jpegQuality); break; case IMAGETYPE_GIF: imagegif($thumb, $target); break; } imagedestroy($thumb); return $target; }
public function dump($var) { $result = array(); $gd_info = gd_info(); $width = imagesx($var); $height = imagesy($var); $colors_palette = imagecolorstotal($var); $is_true_color = imageistruecolor($var) ? 'TRUE' : 'FALSE'; ob_start(); imagepng($var); $image = ob_get_clean(); $gd_support = array(); if ($gd_info['FreeType Support']) { $gd_support[] = 'FreeType(' . $gd_info['FreeType Linkage'] . ')'; } if ($gd_info['T1Lib Support']) { $gd_support[] = 'T1Lib'; } if ($gd_info['GIF Read Support'] || $gd_info['GIF Create Support']) { if ($gd_info['GIF Read Support'] && $gd_info['GIF Create Support']) { $gd_support[] = 'GIF'; } elseif ($gd_info['GIF Read Support']) { $gd_support[] = 'GIF(read)'; } elseif ($gd_info['GIF Create Support']) { $gd_support[] = 'GIF(create)'; } } if ($gd_info['JPEG Support']) { $gd_support[] = 'JPEG'; } if ($gd_info['PNG Support']) { $gd_support[] = 'PNG'; } if ($gd_info['WBMP Support']) { $gd_support[] = 'WBMP'; } if ($gd_info['XPM Support']) { $gd_support[] = 'XPM'; } if ($gd_info['XBM Support']) { $gd_support[] = 'XBM'; } if ($gd_info['JIS-mapped Japanese Font Support']) { $gd_support[] = 'JIS-mapped Japanese Font'; } // gd info $result['GD'] = array('version' => $gd_info['GD Version'], 'support' => implode(', ', $gd_support)); // image info $result['image'] = array('width' => $width . 'px', 'height' => $height . 'px', 'colors_palette' => $colors_palette, 'true_color' => $is_true_color, 'image' => 'data:image/png;base64,' . base64_encode($image)); return $result; }
/** * Applies the filter to the resource * * @param ImageResource $aResource */ public function applyFilter(ImageResource $aResource) { $dest = $aResource->getResource(); if (imageistruecolor($dest)) { imagetruecolortopalette($dest, false, 256); } foreach ($this->search as $search) { $searchRgb = new Color($search); $index = imagecolorclosest($aResource->getResource(), $searchRgb->getRed(), $searchRgb->getGreen(), $searchRgb->getBlue()); // get White COlor imagecolorset($aResource->getResource(), $index, $this->replace->getRed(), $this->replace->getGreen(), $this->replace->getBlue()); // SET NEW COLOR } $aResource->setResource($dest); }
/** * Read color information from a certain position * * @param \Intervention\Image\Image $image * @return boolean */ public function execute($image) { $x = $this->argument(0)->type('digit')->required()->value(); $y = $this->argument(1)->type('digit')->required()->value(); $format = $this->argument(2)->type('string')->value('array'); // pick color $color = imagecolorat($image->getCore(), $x, $y); if (!imageistruecolor($image->getCore())) { $color = imagecolorsforindex($image->getCore(), $color); $color['alpha'] = round(1 - $color['alpha'] / 127, 2); } $color = new Color($color); // format to output $this->setOutput($color->format($format)); return true; }
/** * Loads a GD resource from a file. * * @return bool * TRUE or FALSE, based on success. */ protected function load() { $function = 'imagecreatefrom' . image_type_to_extension($this->getType(), FALSE); if (function_exists($function) && ($resource = $function($this->getImage()->getSource()))) { $this->setResource($resource); if (!imageistruecolor($resource)) { // Convert indexed images to true color, so that filters work // correctly and don't result in unnecessary dither. $new_image = $this->createTmp($this->getType(), imagesx($resource), imagesy($resource)); imagecopy($new_image, $resource, 0, 0, 0, 0, imagesx($resource), imagesy($resource)); imagedestroy($resource); $this->setResource($new_image); } return (bool) $this->getResource(); } return FALSE; }
function lsb_gen_image($solution) { if (false === ($image = imagecreatefrompng(LSB_IMAGE_PATH))) { return GWF_WEB_ROOT . 'error/Can_not_create_img'; } if (false === imageistruecolor($image)) { return GWF_WEB_ROOT . 'error/Image_not_true_color'; } lsb_write_string($image, 0, $solution); // lsb_write_string($image, 1, $solution); // lsb_write_string($image, 2, $solution); $sessid = GWF_Session::getSessID(); $out_path = "challenge/training/stegano/LSB/temp/{$sessid}.png"; // $out_path = 'dbimg/lsb/'.$sessid.'.png'; imagepng($image, $out_path); imagedestroy($image); return $out_path; }
function Draw($text) { $font = dirname(__FILE__) . "/captcha/font.ttf"; $fontSize = 20; $width = ($fontSize - 5) * strlen($text) + 40; $height = $fontSize + 30; $img = imagecreatetruecolor($width, $height); imagealphablending($img, true); imagesavealpha($img, true); $r = mt_rand(20, 235); $b = mt_rand(20, 235); $g = mt_rand(20, 235); $bgcolor = imagecolorallocatealpha($img, $r, $g, $b, 0); imagefill($img, 1, 1, $bgcolor); $ex = mt_rand(15, 20); if (mt_rand(0, 1)) { $ex = -$ex; } $b += $ex; $r += $ex; $g += $ex; $color = imagecolorallocate($img, $r, $b, $g); $text = explode(" ", $text); $sumWidth = 0; $r -= 2 * ex; $b -= 2 * ex; $g -= 2 * ex; $elcolor = imagecolorallocatealpha($img, $r, $g, $b, 20); $ellipseWidth = mt_rand(30, $width - 50); $ellipsePosX = mt_rand($ellipseWidth, $width - $ellipseWidth); $ellipseHeight = mt_rand(10, 40); $ellipsePosY = mt_rand($ellipseHeight, $height - $ellipseHeight); imageellipse($img, $ellipsePosX, $ellipsePosY, $ellipseWidth, $ellipseHeight, $elcolor); imagefill($img, 20, 30, $elcolor); for ($i = 0; $i < count($text); ++$i) { imagettftext($img, $fontSize, mt_rand(-5, 5), 10 + ($fontSize - 8) * $sumWidth + 20 * $i, $fontSize + mt_rand(-10, 10) + 10, $color, $font, $text[$i]); $sumWidth += strlen($text[$i]); } if (imageistruecolor($img)) { header("content-type: image/png"); echo imagepng($img); } exit; }
protected function setlayersResource() { $this->layersresource = imagecreatetruecolor($this->background->width, $this->background->height); $backgroundindex = imagecolorallocatealpha($this->layersresource, 255, 255, 255, 127); imagecolortransparent($this->layersresource, $backgroundindex); imagefill($this->layersresource, 0, 0, $backgroundindex); foreach ($this->layers as $layer) { $args = $this->getCopyArgs($this->background->width, $this->background->height, $layer->width, $layer->height, $layer->alignment, $layer->x, $layer->y); if ($args === false) { continue; } if ($layer::TYPE === 'image') { if ($this->format === 'gif' && $layer->format === 'png' && imageistruecolor($layer->resource) === true) { $this->renderAlphaForPalette($this->layersresource, $layer->resource, $args['dstx'], $args['dsty'], $args['srcx'], $args['srcy'], $args['srcw'], $args['srch']); } } imagecopy($this->layersresource, $layer->resource, $args['dstx'], $args['dsty'], $args['srcx'], $args['srcy'], $args['srcw'], $args['srch']); } }
public function __construct($imagePath, $map = self::WHITE_MAP) { if (!file_exists($imagePath)) { throw new Exception("Path was not found."); } //var_dump(realpath($imagePath));exit; $this->name = basename(realpath($imagePath)); $this->x = 0; $this->y = 0; $imageInfo = getimagesize($imagePath); $this->w = $imageInfo[0]; $this->h = $imageInfo[1]; $this->path = $imagePath; $this->colorMap = $map === self::BLACK_MAP ? self::$BLACK_COLOR_MAP : self::$WHITE_COLOR_MAP; $this->image = imagecreatefromstring(file_get_contents($imagePath)); $this->is32Bit = imageistruecolor($this->image); imagepalettetotruecolor($this->image); imagealphablending($this->image, false); imagesavealpha($this->image, true); }
/** * Construct * * @param resource $resource * @throws InvalidArgumentException */ public function __construct($resource) { if (!$this->isInstalled()) { throw new RuntimeException('GD library not installed.'); } if (!version_compare($this->getVersion(), self::VERSION_REQUIRED, '>=')) { throw new RuntimeException(sprintf('GD library is installed but version is lower than required (>= %s).', self::VERSION_REQUIRED)); } if (!is_resource($resource) || get_resource_type($resource) !== 'gd') { throw new InvalidArgumentException('Argument is not a valid GD resource.'); } if (!imageistruecolor($resource)) { $width = imagesx($resource); $height = imagesy($resource); $truecolor = imagecreatetruecolor($width, $height); imagecopymerge($truecolor, $resource, 0, 0, 0, 0, $width, $height, 100); imagedestroy($resource); $this->resource = $truecolor; } else { $this->resource = $resource; } }
function getPixel($x, $y) { /* Make sure we have an image */ if (!$this->image) { trigger_error("No image is set", E_USER_WARNING); return false; } $color = @imagecolorat($this->image, $x, $y); if ($color == false) { return false; } /* Determine if we are using true colors or indexed colors */ if (imageistruecolor($this->image)) { /* Color components */ $red = $color >> 16 & 255; $green = $color >> 8 & 255; $blue = $color & 255; $return_value = array('red' => $red, 'green' => $green, 'blue' => $blue); } else { $return_value = imagecolorsforindex($this->image, $color); } return $return_value; }