function image_create() { header("Content-type: image/png"); $string = $_GET['text']; $font = imageloadfont("font.gdf"); $im = imagecreatefrompng("captsource.png"); $color[0] = imagecolorallocate($im, 0, 0, 0); $color[1] = imagecolorallocate($im, 192, 192, 192); $color[2] = imagecolorallocate($im, 255, 255, 00); $color[3] = imagecolorallocate($im, 00, 128, 255); $A = rand(0, 9); //Диапазон для A $B = rand(0, 9); //Диапазон для B $answer = 0; if ($B > $A) { $tmp = $B; $B = $A; $A = $tmp; } imagestring($im, $font, 5, rand(0, 2), $A, $color[rand(0, 3)]); if (rand(0, 1)) { imagestring($im, $font, 25, rand(0, 4), '+', $color[2]); $answer = $A + $B; } else { imagestring($im, $font, 25, rand(0, 4), '-', $color[rand(0, 3)]); $answer = $A - $B; } imagestring($im, $font, 50, rand(0, 2), $B, $color[rand(0, 3)]); imagestring($im, $font, 78, 1, '=', $color[0]); imagepng($im); imagedestroy($im); $_SESSION['answer'] = $answer; }
public function setFont($fuente) { if (is_nan($fuente)) { $fuente = imageloadfont($fuente); } $this->fuente = $fuente; }
private function generate() { $this->image = imagecreate($this->width, $this->height); imagecolorallocate($this->image, 200, 200, 200); $text_color = imagecolorallocate($this->image, 50, 50, 50); $this->jamm(); $font = imageloadfont(Kennel::$ROOT_PATH . '/system/assets/files/dots.gdf'); imagestring($this->image, $font, 5, 5, $this->text, $text_color); }
function __construct($face, $color) { if (is_int($face) && $face >= 1 && $face <= 5) { $this->font = $face; } else { $this->font = imageloadfont($face); } $this->color = $color; }
function add_ticket($var_filename, $number, $dest, $text, $fax_img = null) { unset($exec_output); unset($exec_return); $convert_cmd = "/usr/bin/convert {$var_filename} " . IMAGEN_FAX; if (file_exists($var_filename)) { exec($convert_cmd, $exec_output, $exec_return); } if (file_exists(IMAGEN_FAX)) { $files .= IMAGEN_FAX . " "; echo "entro 19"; if (!function_exists('imagecreatefromjpeg')) { die("no existe"); } $image = imagecreatefromjpeg(IMAGEN_FAX); $fuente1 = imageloadfont("arialns.gdf"); $fuente2 = imageloadfont("arial.gdf"); $fuente3 = imageloadfont("code.gdf"); imagestring($image, $fuente1, 10, 10, " Al contestar por favor cite estos datos", $negro); imagestring($image, $fuente2, 10, 30, " Fecha de Radicado: " . date('d-m-Y') . "", $negro); imagestring($image, $fuente2, 10, 50, " No. de Radicado:20075000000491", $negro); imagestring($image, $fuente3, 10, 70, " 20075000000491", $negro); imagejpeg($image, IMAGEN_FAX, 100); } else { $i = 0; while (file_exists(IMAGEN_FAX . ".{$i}")) { $image = imagecreatefrompng(IMAGEN_FAX . ".{$i}"); $negro = imagecolorallocate($image, 0, 0, 0); $x = imagesx($image); imagerectangle($image, $x / 2 - 5, 90, $x / 2 + 240, 170, $negro); imagestring($image, 5, $x / 2, 100, "SUPERSOLIDARIA Rad No. {$number}", $negro); imagestring($image, 5, $x / 2, 120, "{$text}", $negro); imagestring($image, 5, $x / 2, 140, "Gestion Documental Orfeo", $negro); imagepng($image, IMAGEN_FAX . ".{$i}"); $files .= IMAGEN_FAX . ".{$i} "; $i++; imagedestroy($image); } echo "entro else fax_temp"; } unset($exec_output); unset($exec_return); $convert = "/usr/bin/convert -adjoin {$files} {$dest}"; exec($convert, $exec_output, $exec_return); echo $convert; if (file_exists(IMAGEN_FAX)) { exec("rm -rf " . IMAGEN_FAX); } else { $i = 0; while (file_exists(IMAGEN_FAX . ".{$i}")) { exec("rm -rf " . IMAGEN_FAX . ".{$i}"); $i++; } } }
function createImage() { $rgb = explode(',', $this->textcolor); $im = imagecreatetruecolor($this->width, $this->height); $text_color = imagecolorallocate($im, $rgb[0], $rgb[1], $rgb[2]); $font = imageloadfont('./Army.ttf'); imagestring($im, $font, 100, 105, $this->slidetext, $text_color); // Set the content type header - in this case image/jpeg header('Content-Type: image/jpeg'); // Output the image imagejpeg($im, './slides/slide1.jpeg'); // Free up memory imagedestroy($im); }
function kleeja_cpatcha_image() { //Let's generate a totally random string using md5 $md5_hash = md5(rand(0, 999)); //I think the bad things in captcha is two things, O and 0 , so let's remove zero. $security_code = str_replace('0', '', $md5_hash); //We don't need a 32 character long string so we trim it down to 5 $security_code = substr($security_code, 15, 5); //Set the session to store the security code $_SESSION["klj_sec_code"] = $security_code; //Set the image width and height $width = 150; $height = 25; //Create the image resource $image = ImageCreate($width, $height); //We are making three colors, white, black and gray $white = ImageColorAllocate($image, 255, 255, 255); $black = ImageColorAllocate($image, rand(0, 100), 0, rand(0, 50)); $grey = ImageColorAllocate($image, 204, 204, 204); //Make the background black ImageFill($image, 0, 0, $black); //options $x = 10; $y = 14; $angle = rand(-7, -10); //Add randomly generated string in white to the image if (function_exists('imagettftext')) { // // We figure a bug that happens when you add font name without './' before it .. // he search in the Linux fonts cache , but when you add './' he will know it's our font. // imagettftext($image, 16, $angle, rand(50, $x), $y + rand(1, 3), $white, './arial.ttf', $security_code); } else { imagestring($image, imageloadfont('arial.gdf'), $x + rand(10, 15), $y - rand(10, 15), $security_code, $white); } //kleeja ! imagestring($image, 1, $width - 35, $height - 10, 'Kleeja', ImageColorAllocate($image, 200, 200, 200)); //Throw in some lines to make it a little bit harder for any bots to break ImageRectangle($image, 0, 0, $width - 1, $height - 1, $grey); imageline($image, 0, $height / 2, $width, $height / 2, $grey); imageline($image, $width / 2, 0, $width / 2, $height, $grey); //Tell the browser what kind of file is come in header("Content-Type: image/png"); //Output the newly created image in jpeg format ImagePng($image); //Free up resources ImageDestroy($image); }
function display() { $width = 26 * $this->size; $height = 50; $string = $this->randStr(); $im = ImageCreate($width, $height); $imBG = imagecreatefromjpeg(dirname(__FILE__) . "/captcha/images/captcha.jpg"); $bg = imagecolorallocate($im, 255, 255, 255); $black = imagecolorallocate($im, 0, 0, 0); $grey = imagecolorallocate($im, 170, 170, 170); imagerectangle($im, 0, 0, $width - 1, $height - 1, $grey); $font = imageloadfont(dirname(__FILE__) . "/captcha/font/anonymous.gdf"); imagestring($im, $font, $this->size, 5, $string, $black); imagecopymerge($im, $imBG, 0, 0, 0, 0, 256, 256, 55); imagepng($im); imagedestroy($im); }
public function __construct($aWidth = 0, $aHeight = 0, $aFormat = DEFAULT_GFORMAT, $aSetAutoMargin = true) { $this->original_width = $aWidth; $this->original_height = $aHeight; $this->CreateImgCanvas($aWidth, $aHeight); if ($aSetAutoMargin) { $this->SetAutoMargin(); } if (!$this->SetImgFormat($aFormat)) { Util\JpGraphError::RaiseL(25081, $aFormat); //("JpGraph: Selected graphic format is either not supported or unknown [$aFormat]"); } $this->ttf = new TTF(); $this->langconv = new LanguageConv(); $this->ff_font0 = imageloadfont(dirname(dirname(__FILE__)) . "/fonts/FF_FONT0.gdf"); $this->ff_font1 = imageloadfont(dirname(dirname(__FILE__)) . "/fonts/FF_FONT1.gdf"); $this->ff_font2 = imageloadfont(dirname(dirname(__FILE__)) . "/fonts/FF_FONT2.gdf"); $this->ff_font1_bold = imageloadfont(dirname(dirname(__FILE__)) . "/fonts/FF_FONT1-Bold.gdf"); $this->ff_font2_bold = imageloadfont(dirname(dirname(__FILE__)) . "/fonts/FF_FONT2-Bold.gdf"); }
public function show() { Response::getInstance()->setContent('image/png'); $str = $this->randomString(); Session::getInstance()->Captcha = $str; $image_x = 100; $image_y = 40; $im = imagecreatetruecolor($image_x, $image_y); $bgcoolor = imagecolorallocate($im, rand(177, 255), rand(177, 255), rand(177, 255)); imagefill($im, 0, 0, $bgcoolor); $cl = imagecolorallocate($im, rand(0, 100), rand(0, 100), rand(0, 100)); $f = imageloadfont('hadi.gdf'); imagestring($im, $f, 10, 10, $str, $cl); $lineCount = rand(7, 12); while ($lineCount--) { $cl = imagecolorallocate($im, rand(0, 127), rand(0, 127), rand(0, 127)); imageline($im, rand(0, $image_x), rand(0, $image_y), rand(0, $image_x), rand(0, $image_y), $cl); } imagepng($im); }
function createImage($width = 135, $height = 45) { // send header for our image header("Content-type:image/jpeg"); // create an image $im = imagecreate($width, $height); // white background $black = imagecolorallocate($im, 0, 0, 0); // black text and grid $white = imagecolorallocate($im, 255, 255, 255); $other = imagecolorallocate($im, 0, 0, 255); // get a random number to start drawing out grid from $num = rand(0, 5); // draw vertical bars for ($i = $num; $i <= $width; $i += 10) { imageline($im, $i, 0, $i, 45, $other); } // draw horizontal bars for ($i = $num; $i <= $height + 10; $i += 10) { imageline($im, 0, $i, 135, $i, $other); } // generate a random string $string = substr(strtolower(md5(uniqid(rand(), 1))), 0, 7); $string = str_replace('2', 'a', $string); $string = str_replace('l', 'p', $string); $string = str_replace('1', 'h', $string); $string = str_replace('0', 'y', $string); $string = str_replace('o', 'y', $string); // place this string into the image $font = imageloadfont('anticlimax.gdf'); imagestring($im, $font, 10, 10, $string, $white); // create the image and send to browser imagejpeg($im); // destroy the image imagedestroy($im); // return the random text generated return $this->text = $string; }
function abatly_captcha() { $md5_hash = md5(rand(0, 999)); $security_code = str_replace('0', '', $md5_hash); $security_code = substr($security_code, 15, 5); $_SESSION["security_code"] = $security_code; $width = 128; $height = 40; $image = ImageCreate($width, $height); $white = ImageColorAllocate($image, 255, 255, 255); $black = ImageColorAllocate($image, rand(0, 100), 0, rand(0, 50)); $grey = ImageColorAllocate($image, 204, 204, 204); $red = imagecolorallocatealpha($image, 255, 0, 0, 75); $green = imagecolorallocatealpha($image, 0, 255, 0, 75); $blue = imagecolorallocatealpha($image, 0, 0, 255, 75); imagefilledrectangle($image, 0, 0, $width, $height, $white); imagefilledellipse($image, ceil(rand(5, 145)), ceil(rand(0, 35)), 30, 30, $red); imagefilledellipse($image, ceil(rand(5, 145)), ceil(rand(0, 35)), 30, 30, $green); imagefilledellipse($image, ceil(rand(5, 145)), ceil(rand(0, 35)), 30, 30, $blue); imagefilledrectangle($image, 0, 0, $width, 0, $black); imagefilledrectangle($image, $width - 1, 0, $width - 1, $height - 1, $black); imagefilledrectangle($image, 0, 0, 0, $height - 1, $black); imagefilledrectangle($image, 0, $height - 1, $width, $height - 1, $black); ImageFill($image, 0, 0, $black); $x = 20; $y = 24; $angle = rand(-7, -10); if (function_exists('imagettftext')) { imagettftext($image, 20, $angle, rand(20, $x), $y + rand(1, 3), $black, 'arial.ttf', $security_code); } else { imagestring($image, imageloadfont('arial.gdf'), $x + rand(10, 15), $y - rand(10, 15), $security_code, $white); } ImageRectangle($image, 0, 0, $width - 1, $height - 1, $black); imageline($image, $width / 2, 0, $width / 2, $height, $black); header("Content-Type: image/png"); ImagePng($image); ImageDestroy($image); }
function imgtxt_mk($t, $fx, $fy, $lac, $hac, $fnt, $clr, $dest) { $t = str_replace(" ", ' ', $t); $nb_chars = strlen($t); $width = 400; //currentwidth(); if ($lac && $width) { $maxl = floor($width / $lac); } else { $maxl = 400; } $la = $nb_chars * $lac; $la = $la > $width - 8 ? $width - 8 : $la; if ($nb_chars > $maxl or strpos($t, "\n") !== false) { $r = gdf_nblines($t, $maxl); } //$r=imt_mk($t); $ha = $r ? $hac * count($r) : ($hac ? $hac : 20); $clr = $clr ? $clr : '000000'; $rh = hexdec(substr($clr, 0, 2)); $gh = hexdec(substr($clr, 2, 2)); $bh = hexdec(substr($clr, 4, 2)); $image = imagecreate($la, $ha); $blanc = imagecolorallocate($image, 255, 255, 255); $color = imagecolorallocate($image, $rh, $gh, $bh); $font = imageloadfont($fnt); if ($r) { foreach ($r as $k => $v) { $fx = $k * lac; $fy = $k * $hac; imagestring($image, $font, 1, $fy, $v, $color); } } else { imagestring($image, $font, $fx ? $fx : 0, $fy ? $fy : 0, $t, $color); } imagecolortransparent($image, $blanc); imagepng($image, $dest); }
function generateValidationImage($rand) { global $site_font_path; global $site_font_validation; $width = 120; $height = 40; $image = imagecreate($width, $height); $bgColor = imagecolorallocate($image, 255, 255, 255); $textColor = imagecolorallocate($image, 0, 0, 0); // Add Random noise for ($i = 0; $i < 250; $i++) { $rx1 = rand(0, $width); $rx2 = rand(0, $width); $ry1 = rand(0, $height); $ry2 = rand(0, $height); $rcVal = rand(0, 255); $rc1 = imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(100, 255)); imageline($image, $rx1, $ry1, $rx2, $ry2, $rc1); } // write the random number $font = imageloadfont($site_font_path . "/" . $site_font_validation); imagestring($image, $font, 3, 0, $rand, $textColor); // send several headers to make sure the image is not cached // Date in the past header("Expires: Mon, 23 Jul 1993 05:00:00 GMT"); // always modified header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // HTTP/1.1 header("Cache-Control: no-store, no-cache, must-revalidate"); header("Cache-Control: post-check=0, pre-check=0", false); // HTTP/1.0 header("Pragma: no-cache"); // send the content type header so the image is displayed properly header('Content-type: image/jpeg'); imagejpeg($image); imagedestroy($image); }
/** * Actually uploads the file, and act on it according to the set processing class variables * * This function copies the uploaded file to the given location, eventually performing actions on it. * Typically, you can call {@link process} several times for the same file, * for instance to create a resized image and a thumbnail of the same file. * The original uploaded file remains intact in its temporary location, so you can use {@link process} several times. * You will be able to delete the uploaded file with {@link clean} when you have finished all your {@link process} calls. * * According to the processing class variables set in the calling file, the file can be renamed, * and if it is an image, can be resized or converted. * * When the processing is completed, and the file copied to its new location, the * processing class variables will be reset to their default value. * This allows you to set new properties, and perform another {@link process} on the same uploaded file * * It will set {@link processed} (and {@link error} is an error occurred) * * @access public * @param string $server_path Path location of the uploaded file, with an ending slash */ function process($server_path) { $this->error = ''; $this->processed = true; if (strtolower(substr(PHP_OS, 0, 3)) === 'win') { if (substr($server_path, -1, 1) != '\\') { $server_path = $server_path . '\\'; } } else { if (substr($server_path, -1, 1) != '/') { $server_path = $server_path . '/'; } } $this->log .= '<b>' . _("process file to") . ' ' . $server_path . '</b><br />'; // checks file size and mine type if ($this->uploaded) { if ($this->file_src_size > $this->file_max_size) { $this->processed = false; $this->error = _(MSG019); } else { $this->log .= '- ' . _("file size OK") . '<br />'; } // turn dangerous scripts into text files if ($this->no_script) { if ((substr($this->file_src_mime, 0, 5) == 'text/' || strpos($this->file_src_mime, 'javascript') !== false) && substr($this->file_src_name, -4) != '.txt' || preg_match('/\\.(php|pl|py|cgi|asp)$/i', $this->file_src_name) || empty($this->file_src_name_ext)) { $this->file_src_mime = 'text/plain'; $this->log .= '- ' . _("script") . ' ' . $this->file_src_name . ' ' . _("renamed as") . ' ' . $this->file_src_name . '.txt!<br />'; $this->file_src_name_ext .= empty($this->file_src_name_ext) ? 'txt' : '.txt'; } } // checks MIME type with mime_magic if ($this->mime_magic_check && function_exists('mime_content_type')) { $detected_mime = mime_content_type($this->file_src_pathname); if ($this->file_src_mime != $detected_mime) { $this->log .= '- ' . _("MIME type detected as") . ' ' . $detected_mime . ' ' . _("but given as") . ' ' . $this->file_src_mime . '!<br />'; $this->file_src_mime = $detected_mime; } } if ($this->mime_check && empty($this->file_src_mime)) { $this->processed = false; $this->error = _("MIME type can't be detected!"); } else { if ($this->mime_check && !empty($this->file_src_mime) && !array_key_exists($this->file_src_mime, array_flip($this->allowed))) { $this->processed = false; $this->error = _("Incorrect type of file"); } else { $this->log .= '- ' . _("file mime OK") . ' : ' . $this->file_src_mime . '<br />'; } } } else { $this->error = _("File not uploaded. Can't carry on a process"); $this->processed = false; } if ($this->processed) { $this->file_dst_path = $server_path; // repopulate dst variables from src $this->file_dst_name = $this->file_src_name; $this->file_dst_name_body = $this->file_src_name_body; $this->file_dst_name_ext = $this->file_src_name_ext; if ($this->file_new_name_body != '') { // rename file body $this->file_dst_name_body = $this->file_new_name_body; $this->log .= '- ' . _("new file name body") . ' : ' . $this->file_new_name_body . '<br />'; } if ($this->file_new_name_ext != '') { // rename file ext $this->file_dst_name_ext = $this->file_new_name_ext; $this->log .= '- ' . _("new file name ext") . ' : ' . $this->file_new_name_ext . '<br />'; } if ($this->file_name_body_add != '') { // append a bit to the name $this->file_dst_name_body = $this->file_dst_name_body . $this->file_name_body_add; $this->log .= '- ' . _("file name body add") . ' : ' . $this->file_name_body_add . '<br />'; } if ($this->file_safe_name) { // formats the name $this->file_dst_name_body = str_replace(array(' ', '-'), array('_', '_'), $this->file_dst_name_body); $this->file_dst_name_body = ereg_replace('[^A-Za-z0-9_]', '', $this->file_dst_name_body); $this->log .= '- ' . _("file name safe format") . '<br />'; } $this->log .= '- ' . _("destination variables") . '<br />'; $this->log .= ' file_dst_path : ' . $this->file_dst_path . '<br />'; $this->log .= ' file_dst_name_body : ' . $this->file_dst_name_body . '<br />'; $this->log .= ' file_dst_name_ext : ' . $this->file_dst_name_ext . '<br />'; // do we do some image manipulation? $image_manipulation = $this->image_resize || $this->image_convert != '' || is_numeric($this->image_brightness) || is_numeric($this->image_contrast) || is_numeric($this->image_threshold) || !empty($this->image_tint_color) || !empty($this->image_overlay_color) || !empty($this->image_text) || $this->image_greyscale || $this->image_negative || !empty($this->image_watermark) || is_numeric($this->image_rotate) || is_numeric($this->jpeg_size) || !empty($this->image_flip) || !empty($this->image_crop) || !empty($this->image_border) || $this->image_frame > 0 || $this->image_bevel > 0 || $this->image_reflection_height; if ($image_manipulation) { if ($this->image_convert == '') { $this->file_dst_name = $this->file_dst_name_body . (!empty($this->file_dst_name_ext) ? '.' . $this->file_dst_name_ext : ''); $this->log .= '- ' . _("image operation, keep extension") . '<br />'; } else { $this->file_dst_name = $this->file_dst_name_body . '.' . $this->image_convert; $this->log .= '- ' . _("image operation, change extension for conversion type") . '<br />'; } } else { $this->file_dst_name = $this->file_dst_name_body . (!empty($this->file_dst_name_ext) ? '.' . $this->file_dst_name_ext : ''); $this->log .= '- ' . _("no image operation, keep extension") . '<br />'; } if (!$this->file_auto_rename) { $this->log .= '- ' . _("no auto_rename if same filename exists") . '<br />'; $this->file_dst_pathname = $this->file_dst_path . $this->file_dst_name; } else { $this->log .= '- ' . _("checking for auto_rename") . '<br />'; $this->file_dst_pathname = $this->file_dst_path . $this->file_dst_name; $body = $this->file_dst_name_body; $cpt = 1; while (@file_exists($this->file_dst_pathname)) { $this->file_dst_name_body = $body . '_' . $cpt; $this->file_dst_name = $this->file_dst_name_body . (!empty($this->file_dst_name_ext) ? '.' . $this->file_dst_name_ext : ''); $cpt++; $this->file_dst_pathname = $this->file_dst_path . $this->file_dst_name; } if ($cpt > 1) { $this->log .= ' ' . _("auto_rename to") . ' ' . $this->file_dst_name . '<br />'; } } $this->log .= '- ' . _("destination file details") . '<br />'; $this->log .= ' file_dst_name : ' . $this->file_dst_name . '<br />'; $this->log .= ' file_dst_pathname : ' . $this->file_dst_pathname . '<br />'; if ($this->file_overwrite) { $this->log .= '- ' . _("no overwrite checking") . '<br />'; } else { if (@file_exists($this->file_dst_pathname)) { $this->processed = false; $this->error = $this->file_dst_name . ' ' . _("already exists. Please change the file name"); } else { $this->log .= '- ' . $this->file_dst_name . ' ' . _("doesn't exist already") . '<br />'; } } } else { $this->processed = false; } if (!$this->no_upload_check && !is_uploaded_file($this->file_src_pathname)) { $this->processed = false; $this->error = _("No correct source file. Can't carry on a process"); } // checks if the destination directory exists, and attempt to create it if ($this->processed && !file_exists($this->file_dst_path)) { if ($this->dir_auto_create) { $this->log .= '- ' . $this->file_dst_path . ' ' . _("doesn't exist. Attempting creation:"); if (!$this->r_mkdir($this->file_dst_path, $this->dir_chmod)) { $this->log .= ' ' . _("failed") . '<br />'; $this->processed = false; $this->error = _("Destination directory can't be created. Can't carry on a process"); } else { $this->log .= ' ' . _("success") . '<br />'; } } else { $this->error = _("Destination directory doesn't exist. Can't carry on a process"); } } if ($this->processed && !is_dir($this->file_dst_path)) { $this->processed = false; $this->error = _("Destination path is not a directory. Can't carry on a process"); } // checks if the destination directory is writeable, and attempt to make it writeable $hash = md5($this->file_dst_name_body . rand(1, 1000)); if ($this->processed && !($f = @fopen($this->file_dst_path . $hash . '.' . $this->file_dst_name_ext, 'a+'))) { if ($this->dir_auto_chmod) { $this->log .= '- ' . $this->file_dst_path . ' ' . _("is not writeable. Attempting chmod:"); if (!@chmod($this->file_dst_path, $this->dir_chmod)) { $this->log .= ' ' . _("failed") . '<br />'; $this->processed = false; $this->error = _("Destination directory can't be made writeable. Can't carry on a process"); } else { $this->log .= ' ' . _("success") . '<br />'; if (!($f = @fopen($this->file_dst_path . $hash . '.' . $this->file_dst_name_ext, 'a+'))) { // we re-check $this->processed = false; $this->error = _("Destination directory is still not writeable. Can't carry on a process"); } else { @fclose($f); } } } else { $this->processed = false; $this->error = _("Destination path is not a writeable. Can't carry on a process"); } } else { @fclose($f); @unlink($this->file_dst_path . $hash . '.' . $this->file_dst_name_ext); } if ($this->processed) { if ($image_manipulation) { // we have a writeable destination, but we need to check if we can read the file directly // if we can't (open_basedir, etc...), we will copy it with a temporary name $has_temp_file = false; if ($this->processed && !file_exists($this->file_src_pathname)) { $this->log .= '- ' . _("can't directly access the uploaded file" . '<br />'); $this->log .= ' ' . _("attempting creating a temp file:"); $hash = md5($this->file_dst_name_body . rand(1, 1000)); if (move_uploaded_file($this->file_src_pathname, $this->file_dst_path . $hash . '.' . $this->file_dst_name_ext)) { $this->file_src_pathname = $this->file_dst_path . $hash . '.' . $this->file_dst_name_ext; $this->log .= ' ' . _("file created") . '<br />'; $this->log .= ' ' . _("temp file is:") . ' ' . $this->file_src_pathname . '<br />'; $has_temp_file = true; } else { $this->log .= ' ' . _("failed") . '<br />'; $this->processed = false; $this->error = _("Can't create the temporary file. Can't carry on a process"); } } // checks if the source file is readable if ($this->processed && !($f = @fopen($this->file_src_pathname, 'r'))) { $this->processed = false; $this->error = _("Source file is not readable. Can't carry on a process"); } else { @fclose($f); } // we now do all the image manipulations $this->log .= '- ' . _("image resizing or conversion wanted") . '<br />'; if ($this->gd_version()) { switch ($this->file_src_mime) { case 'image/pjpeg': case 'image/jpeg': case 'image/jpg': if (!function_exists('imagecreatefromjpeg')) { $this->processed = false; $this->error = _("No create from JPEG support"); } else { $image_src = @imagecreatefromjpeg($this->file_src_pathname); if (!$image_src) { $this->processed = false; $this->error = _("Error in creating JPEG image from source"); } else { $this->log .= '- ' . _("source image is JPEG") . '<br />'; } } break; case 'image/x-png': case 'image/png': if (!function_exists('imagecreatefrompng')) { $this->processed = false; $this->error = _("No create from PNG support"); } else { $image_src = @imagecreatefrompng($this->file_src_pathname); if (!$image_src) { $this->processed = false; $this->error = _("Error in creating PNG image from source"); } else { $this->log .= '- ' . _("source image is PNG") . '<br />'; } } break; case 'image/gif': if (!function_exists('imagecreatefromgif')) { $this->processed = false; $this->error = _("Error in creating GIF image from source"); } else { $image_src = @imagecreatefromgif($this->file_src_pathname); if (!$image_src) { $this->processed = false; $this->error = _("No GIF read support"); } else { $this->log .= '- ' . _("source image is GIF") . '<br />'; } } break; default: $this->processed = false; $this->error = _(MSG012); } } else { $this->processed = false; $this->error = _("GD doesn't seem to be present"); } if ($this->processed && $image_src) { $this->image_src_x = imagesx($image_src); $this->image_src_y = imagesy($image_src); $this->image_dst_x = $this->image_src_x; $this->image_dst_y = $this->image_src_y; $gd_version = $this->gd_version(); $ratio_crop = null; if ($this->image_resize) { $this->log .= '- ' . _("resizing...") . '<br />'; if ($this->image_ratio_x) { $this->log .= ' ' . _("calculate x size") . '<br />'; $this->image_dst_x = round($this->image_src_x * $this->image_y / $this->image_src_y); $this->image_dst_y = $this->image_y; } else { if ($this->image_ratio_y) { $this->log .= ' ' . _("calculate y size") . '<br />'; $this->image_dst_x = $this->image_x; $this->image_dst_y = round($this->image_src_y * $this->image_x / $this->image_src_x); } else { if ($this->image_ratio || $this->image_ratio_crop || $this->image_ratio_no_zoom_in || $this->image_ratio_no_zoom_out) { $this->log .= ' ' . _("check x/y sizes") . '<br />'; if (!$this->image_ratio_no_zoom_in && !$this->image_ratio_no_zoom_out || $this->image_ratio_no_zoom_in && ($this->image_src_x > $this->image_x || $this->image_src_y > $this->image_y) || $this->image_ratio_no_zoom_out && $this->image_src_x < $this->image_x && $this->image_src_y < $this->image_y) { $this->image_dst_x = $this->image_x; $this->image_dst_y = $this->image_y; if ($this->image_ratio_crop) { if (!is_string($this->image_ratio_crop)) { $this->image_ratio_crop = ''; } $this->image_ratio_crop = strtolower($this->image_ratio_crop); if ($this->image_src_x / $this->image_x > $this->image_src_y / $this->image_y) { $this->image_dst_y = $this->image_y; $this->image_dst_x = intval($this->image_src_x * ($this->image_y / $this->image_src_y)); $ratio_crop = array(); $ratio_crop['x'] = $this->image_dst_x - $this->image_x; if (strpos($this->image_ratio_crop, 'l') !== false) { $ratio_crop['l'] = 0; $ratio_crop['r'] = $ratio_crop['x']; } else { if (strpos($this->image_ratio_crop, 'r') !== false) { $ratio_crop['l'] = $ratio_crop['x']; $ratio_crop['r'] = 0; } else { $ratio_crop['l'] = round($ratio_crop['x'] / 2); $ratio_crop['r'] = $ratio_crop['x'] - $ratio_crop['l']; } } $this->log .= ' ' . _("ratio_crop_x") . ' : ' . $ratio_crop['x'] . ' (' . $ratio_crop['l'] . ';' . $ratio_crop['r'] . ')<br />'; if (is_null($this->image_crop)) { $this->image_crop = array(0, 0, 0, 0); } } else { $this->image_dst_x = $this->image_x; $this->image_dst_y = intval($this->image_src_y * ($this->image_x / $this->image_src_x)); $ratio_crop = array(); $ratio_crop['y'] = $this->image_dst_y - $this->image_y; if (strpos($this->image_ratio_crop, 't') !== false) { $ratio_crop['t'] = 0; $ratio_crop['b'] = $ratio_crop['y']; } else { if (strpos($this->image_ratio_crop, 'b') !== false) { $ratio_crop['t'] = $ratio_crop['y']; $ratio_crop['b'] = 0; } else { $ratio_crop['t'] = round($ratio_crop['y'] / 2); $ratio_crop['b'] = $ratio_crop['y'] - $ratio_crop['t']; } } $this->log .= ' ' . _("ratio_crop_y") . ' : ' . $ratio_crop['y'] . ' (' . $ratio_crop['t'] . ';' . $ratio_crop['b'] . ')<br />'; if (is_null($this->image_crop)) { $this->image_crop = array(0, 0, 0, 0); } } } else { if ($this->image_src_x / $this->image_x > $this->image_src_y / $this->image_y) { $this->image_dst_x = $this->image_x; $this->image_dst_y = intval($this->image_src_y * ($this->image_x / $this->image_src_x)); } else { $this->image_dst_y = $this->image_y; $this->image_dst_x = intval($this->image_src_x * ($this->image_y / $this->image_src_y)); } } } else { $this->log .= ' ' . _("doesn't calculate x/y sizes") . '<br />'; $this->image_dst_x = $this->image_src_x; $this->image_dst_y = $this->image_src_y; } } else { $this->log .= ' ' . _("use plain sizes") . '<br />'; $this->image_dst_x = $this->image_x; $this->image_dst_y = $this->image_y; } } } if ($this->preserve_transparency && $this->file_src_mime != 'image/gif' && $this->file_src_mime != 'image/png') { $this->preserve_transparency = false; } if ($gd_version >= 2 && !$this->preserve_transparency) { $image_dst = imagecreatetruecolor($this->image_dst_x, $this->image_dst_y); } else { $image_dst = imagecreate($this->image_dst_x, $this->image_dst_y); } if ($this->preserve_transparency) { $this->log .= '- ' . _("preserve transparency") . '<br />'; $transparent_color = imagecolortransparent($image_src); imagepalettecopy($image_dst, $image_src); imagefill($image_dst, 0, 0, $transparent_color); imagecolortransparent($image_dst, $transparent_color); } if ($gd_version >= 2 && !$this->preserve_transparency) { $res = imagecopyresampled($image_dst, $image_src, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y, $this->image_src_x, $this->image_src_y); } else { $res = imagecopyresized($image_dst, $image_src, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y, $this->image_src_x, $this->image_src_y); } $this->log .= ' ' . _("resized image object created") . '<br />'; $this->log .= ' image_src_x y : ' . $this->image_src_x . ' x ' . $this->image_src_y . '<br />'; $this->log .= ' image_dst_x y : ' . $this->image_dst_x . ' x ' . $this->image_dst_y . '<br />'; } else { // we only convert, so we link the dst image to the src image $image_dst =& $image_src; } // we have to set image_convert if it is not already if (empty($this->image_convert)) { $this->log .= ' ' . _("setting destination file type to") . ' ' . $this->file_src_name_ext . '<br />'; $this->image_convert = $this->file_src_name_ext; } // crop imag (and also crops if image_ratio_crop is used) if ($gd_version >= 2 && (!empty($this->image_crop) || !is_null($ratio_crop))) { if (is_array($this->image_crop)) { $vars = $this->image_crop; } else { $vars = explode(' ', $this->image_crop); } if (sizeof($vars) == 4) { $ct = $vars[0]; $cr = $vars[1]; $cb = $vars[2]; $cl = $vars[3]; } else { if (sizeof($vars) == 2) { $ct = $vars[0]; $cr = $vars[1]; $cb = $vars[0]; $cl = $vars[1]; } else { $ct = $vars[0]; $cr = $vars[0]; $cb = $vars[0]; $cl = $vars[0]; } } if (strpos($ct, '%') > 0) { $ct = $this->image_dst_y * (str_replace('%', '', $ct) / 100); } if (strpos($cr, '%') > 0) { $cr = $this->image_dst_x * (str_replace('%', '', $cr) / 100); } if (strpos($cb, '%') > 0) { $cb = $this->image_dst_y * (str_replace('%', '', $cb) / 100); } if (strpos($cl, '%') > 0) { $cl = $this->image_dst_x * (str_replace('%', '', $cl) / 100); } if (strpos($ct, 'px') > 0) { $ct = str_replace('px', '', $ct); } if (strpos($cr, 'px') > 0) { $cr = str_replace('px', '', $cr); } if (strpos($cb, 'px') > 0) { $cb = str_replace('px', '', $cb); } if (strpos($cl, 'px') > 0) { $cl = str_replace('px', '', $cl); } $ct = (int) $ct; $cr = (int) $cr; $cb = (int) $cb; $cl = (int) $cl; // we adjust the cropping if we use image_ratio_crop if (!is_null($ratio_crop)) { if (array_key_exists('t', $ratio_crop)) { $ct += $ratio_crop['t']; } if (array_key_exists('r', $ratio_crop)) { $cr += $ratio_crop['r']; } if (array_key_exists('b', $ratio_crop)) { $cb += $ratio_crop['b']; } if (array_key_exists('l', $ratio_crop)) { $cl += $ratio_crop['l']; } } $this->log .= '- ' . _("crop image") . ' : ' . $ct . ' ' . $cr . ' ' . $cb . ' ' . $cl . ' <br />'; $this->image_dst_x = $this->image_dst_x - $cl - $cr; $this->image_dst_y = $this->image_dst_y - $ct - $cb; if ($this->image_dst_x < 1) { $this->image_dst_x = 1; } if ($this->image_dst_y < 1) { $this->image_dst_y = 1; } $tmp = imagecreatetruecolor($this->image_dst_x, $this->image_dst_y); imagecopy($tmp, $image_dst, 0, 0, $cl, $ct, $this->image_dst_x, $this->image_dst_y); // we transfert tmp into image_dst imagedestroy($image_dst); $image_dst = imagecreatetruecolor($this->image_dst_x, $this->image_dst_y); imagecopy($image_dst, $tmp, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y); imagedestroy($tmp); } // flip image if ($gd_version >= 2 && !empty($this->image_flip)) { $this->image_flip = strtolower($this->image_flip); $this->log .= '- ' . _("flip image") . ' : ' . $this->image_flip . '<br />'; $tmp = imagecreatetruecolor($this->image_dst_x, $this->image_dst_y); for ($x = 0; $x < $this->image_dst_x; $x++) { for ($y = 0; $y < $this->image_dst_y; $y++) { if (strpos($this->image_flip, 'v') !== false) { imagecopy($tmp, $image_dst, $this->image_dst_x - $x - 1, $y, $x, $y, 1, 1); } else { imagecopy($tmp, $image_dst, $x, $this->image_dst_y - $y - 1, $x, $y, 1, 1); } } } // we transfert tmp into image_dst imagedestroy($image_dst); $image_dst = imagecreatetruecolor($this->image_dst_x, $this->image_dst_y); imagecopy($image_dst, $tmp, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y); imagedestroy($tmp); } // rotate image if ($gd_version >= 2 && is_numeric($this->image_rotate)) { if (!in_array($this->image_rotate, array(0, 90, 180, 270))) { $this->image_rotate = 0; } if ($this->image_rotate != 0) { if ($this->image_rotate == 90 || $this->image_rotate == 270) { $tmp = imagecreatetruecolor($this->image_dst_y, $this->image_dst_x); } else { $tmp = imagecreatetruecolor($this->image_dst_x, $this->image_dst_y); } $this->log .= '- ' . _("rotate image") . ' : ' . $this->image_rotate . '<br />'; for ($x = 0; $x < $this->image_dst_x; $x++) { for ($y = 0; $y < $this->image_dst_y; $y++) { if ($this->image_rotate == 90) { imagecopy($tmp, $image_dst, $y, $x, $x, $this->image_dst_y - $y - 1, 1, 1); } else { if ($this->image_rotate == 180) { imagecopy($tmp, $image_dst, $x, $y, $this->image_dst_x - $x - 1, $this->image_dst_y - $y - 1, 1, 1); } else { if ($this->image_rotate == 270) { imagecopy($tmp, $image_dst, $y, $x, $this->image_dst_x - $x - 1, $y, 1, 1); } else { imagecopy($tmp, $image_dst, $x, $y, $x, $y, 1, 1); } } } } } if ($this->image_rotate == 90 || $this->image_rotate == 270) { $t = $this->image_dst_y; $this->image_dst_y = $this->image_dst_x; $this->image_dst_x = $t; } // we transfert tmp into image_dst imagedestroy($image_dst); $image_dst = imagecreatetruecolor($this->image_dst_x, $this->image_dst_y); imagecopy($image_dst, $tmp, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y); imagedestroy($tmp); } } // add color overlay if ($gd_version >= 2 && (is_numeric($this->image_overlay_percent) && !empty($this->image_overlay_color))) { $this->log .= '- ' . _("apply color overlay") . '<br />'; sscanf($this->image_overlay_color, "#%2x%2x%2x", $red, $green, $blue); $filter = imagecreatetruecolor($this->image_dst_x, $this->image_dst_y); $color = imagecolorallocate($filter, $red, $green, $blue); imagefilledrectangle($filter, 0, 0, $this->image_dst_x, $this->image_dst_y, $color); imagecopymerge($image_dst, $filter, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y, $this->image_overlay_percent); imagedestroy($filter); } // add brightness, contrast and tint, turns to greyscale and inverts colors if ($gd_version >= 2 && ($this->image_negative || $this->image_greyscale || is_numeric($this->image_threshold) || is_numeric($this->image_brightness) || is_numeric($this->image_contrast) || !empty($this->image_tint_color))) { $this->log .= '- ' . _("apply tint, light, contrast correction, negative, greyscale and threshold") . '<br />'; if (!empty($this->image_tint_color)) { sscanf($this->image_tint_color, "#%2x%2x%2x", $red, $green, $blue); } $background = imagecolorallocatealpha($image_dst, 255, 255, 255, 0); imagefill($image_dst, 0, 0, $background); imagealphablending($image_dst, TRUE); for ($y = 0; $y < $this->image_dst_y; $y++) { for ($x = 0; $x < $this->image_dst_x; $x++) { if ($this->image_greyscale) { $rgb = imagecolorat($image_dst, $x, $y); $pixel = imagecolorsforindex($image_dst, $rgb); $r = $g = $b = round(0.2125 * $pixel['red'] + 0.7154 * $pixel['green'] + 0.0721 * $pixel['blue']); $a = $pixel['alpha']; $pixelcolor = imagecolorallocatealpha($image_dst, $r, $g, $b, $a); imagesetpixel($image_dst, $x, $y, $pixelcolor); } if (is_numeric($this->image_threshold)) { $rgb = imagecolorat($image_dst, $x, $y); $pixel = imagecolorsforindex($image_dst, $rgb); $c = round($pixel['red'] + $pixel['green'] + $pixel['blue']) / 3 - 127; $r = $g = $b = $c > $this->image_threshold ? 255 : 0; $a = $pixel['alpha']; $pixelcolor = imagecolorallocatealpha($image_dst, $r, $g, $b, $a); imagesetpixel($image_dst, $x, $y, $pixelcolor); } if (is_numeric($this->image_brightness)) { $rgb = imagecolorat($image_dst, $x, $y); $pixel = imagecolorsforindex($image_dst, $rgb); $r = max(min(round($pixel['red'] + $this->image_brightness * 2), 255), 0); $g = max(min(round($pixel['green'] + $this->image_brightness * 2), 255), 0); $b = max(min(round($pixel['blue'] + $this->image_brightness * 2), 255), 0); $a = $pixel['alpha']; $pixelcolor = imagecolorallocatealpha($image_dst, $r, $g, $b, $a); imagesetpixel($image_dst, $x, $y, $pixelcolor); } if (is_numeric($this->image_contrast)) { $rgb = imagecolorat($image_dst, $x, $y); $pixel = imagecolorsforindex($image_dst, $rgb); $r = max(min(round(($this->image_contrast + 128) * $pixel['red'] / 128), 255), 0); $g = max(min(round(($this->image_contrast + 128) * $pixel['green'] / 128), 255), 0); $b = max(min(round(($this->image_contrast + 128) * $pixel['blue'] / 128), 255), 0); $a = $pixel['alpha']; $pixelcolor = imagecolorallocatealpha($image_dst, $r, $g, $b, $a); imagesetpixel($image_dst, $x, $y, $pixelcolor); } if (!empty($this->image_tint_color)) { $rgb = imagecolorat($image_dst, $x, $y); $pixel = imagecolorsforindex($image_dst, $rgb); $r = min(round($red * $pixel['red'] / 169), 255); $g = min(round($green * $pixel['green'] / 169), 255); $b = min(round($blue * $pixel['blue'] / 169), 255); $a = $pixel['alpha']; $pixelcolor = imagecolorallocatealpha($image_dst, $r, $g, $b, $a); imagesetpixel($image_dst, $x, $y, $pixelcolor); } if (!empty($this->image_negative)) { $rgb = imagecolorat($image_dst, $x, $y); $pixel = imagecolorsforindex($image_dst, $rgb); $r = round(255 - $pixel['red']); $g = round(255 - $pixel['green']); $b = round(255 - $pixel['blue']); $a = $pixel['alpha']; $pixelcolor = imagecolorallocatealpha($image_dst, $r, $g, $b, $a); imagesetpixel($image_dst, $x, $y, $pixelcolor); } } } } // adds a border if ($gd_version >= 2 && !empty($this->image_border)) { if (is_array($this->image_border)) { $vars = $this->image_border; $this->log .= '- ' . _("add border") . ' : ' . implode(' ', $this->image_border) . '<br />'; } else { $this->log .= '- ' . _("add border") . ' : ' . $this->image_border . '<br />'; $vars = explode(' ', $this->image_border); } if (sizeof($vars) == 4) { $ct = $vars[0]; $cr = $vars[1]; $cb = $vars[2]; $cl = $vars[3]; } else { if (sizeof($vars) == 2) { $ct = $vars[0]; $cr = $vars[1]; $cb = $vars[0]; $cl = $vars[1]; } else { $ct = $vars[0]; $cr = $vars[0]; $cb = $vars[0]; $cl = $vars[0]; } } if (strpos($ct, '%') > 0) { $ct = $this->image_dst_y * (str_replace('%', '', $ct) / 100); } if (strpos($cr, '%') > 0) { $cr = $this->image_dst_x * (str_replace('%', '', $cr) / 100); } if (strpos($cb, '%') > 0) { $cb = $this->image_dst_y * (str_replace('%', '', $cb) / 100); } if (strpos($cl, '%') > 0) { $cl = $this->image_dst_x * (str_replace('%', '', $cl) / 100); } if (strpos($ct, 'px') > 0) { $ct = str_replace('px', '', $ct); } if (strpos($cr, 'px') > 0) { $cr = str_replace('px', '', $cr); } if (strpos($cb, 'px') > 0) { $cb = str_replace('px', '', $cb); } if (strpos($cl, 'px') > 0) { $cl = str_replace('px', '', $cl); } $ct = (int) $ct; $cr = (int) $cr; $cb = (int) $cb; $cl = (int) $cl; $this->image_dst_x = $this->image_dst_x + $cl + $cr; $this->image_dst_y = $this->image_dst_y + $ct + $cb; if (!empty($this->image_border_color)) { sscanf($this->image_border_color, "#%2x%2x%2x", $red, $green, $blue); } $tmp = imagecreatetruecolor($this->image_dst_x, $this->image_dst_y); $background = imagecolorallocatealpha($tmp, $red, $green, $blue, 0); imagefill($tmp, 0, 0, $background); imagecopy($tmp, $image_dst, $cl, $ct, 0, 0, $this->image_dst_x - $cr - $cl, $this->image_dst_y - $cb - $ct); // we transfert tmp into image_dst imagedestroy($image_dst); $image_dst = imagecreatetruecolor($this->image_dst_x, $this->image_dst_y); imagecopy($image_dst, $tmp, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y); imagedestroy($tmp); } // add frame border if (is_numeric($this->image_frame)) { if (is_array($this->image_frame_colors)) { $vars = $this->image_frame_colors; $this->log .= '- ' . _("add frame") . ' : ' . implode(' ', $this->image_frame_colors) . '<br />'; } else { $this->log .= '- ' . _("add frame") . ' : ' . $this->image_frame_colors . '<br />'; $vars = explode(' ', $this->image_frame_colors); } $nb = sizeof($vars); $this->image_dst_x = $this->image_dst_x + $nb * 2; $this->image_dst_y = $this->image_dst_y + $nb * 2; $tmp = imagecreatetruecolor($this->image_dst_x, $this->image_dst_y); imagecopy($tmp, $image_dst, $nb, $nb, 0, 0, $this->image_dst_x - $nb * 2, $this->image_dst_y - $nb * 2); for ($i = 0; $i < $nb; $i++) { sscanf($vars[$i], "#%2x%2x%2x", $red, $green, $blue); $c = imagecolorallocate($tmp, $red, $green, $blue); if ($this->image_frame == 1) { imageline($tmp, $i, $i, $this->image_dst_x - $i - 1, $i, $c); imageline($tmp, $this->image_dst_x - $i - 1, $this->image_dst_y - $i - 1, $this->image_dst_x - $i - 1, $i, $c); imageline($tmp, $this->image_dst_x - $i - 1, $this->image_dst_y - $i - 1, $i, $this->image_dst_y - $i - 1, $c); imageline($tmp, $i, $i, $i, $this->image_dst_y - $i - 1, $c); } else { imageline($tmp, $i, $i, $this->image_dst_x - $i - 1, $i, $c); imageline($tmp, $this->image_dst_x - $nb + $i, $this->image_dst_y - $nb + $i, $this->image_dst_x - $nb + $i, $nb - $i, $c); imageline($tmp, $this->image_dst_x - $nb + $i, $this->image_dst_y - $nb + $i, $nb - $i, $this->image_dst_y - $nb + $i, $c); imageline($tmp, $i, $i, $i, $this->image_dst_y - $i - 1, $c); } } // we transfert tmp into image_dst imagedestroy($image_dst); $image_dst = imagecreatetruecolor($this->image_dst_x, $this->image_dst_y); imagecopy($image_dst, $tmp, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y); imagedestroy($tmp); } // add bevel border if ($this->image_bevel > 0) { if (empty($this->image_bevel_color1)) { $this->image_bevel_color1 = '#FFFFFF'; } if (empty($this->image_bevel_color2)) { $this->image_bevel_color2 = '#000000'; } sscanf($this->image_bevel_color1, "#%2x%2x%2x", $red1, $green1, $blue1); sscanf($this->image_bevel_color2, "#%2x%2x%2x", $red2, $green2, $blue2); imagealphablending($image_dst, true); for ($i = 0; $i < $this->image_bevel; $i++) { $alpha = round($i / $this->image_bevel * 127); $c1 = imagecolorallocatealpha($image_dst, $red1, $green1, $blue1, $alpha); $c2 = imagecolorallocatealpha($image_dst, $red2, $green2, $blue2, $alpha); imageline($image_dst, $i, $i, $this->image_dst_x - $i - 1, $i, $c1); imageline($image_dst, $this->image_dst_x - $i - 1, $this->image_dst_y - $i, $this->image_dst_x - $i - 1, $i, $c2); imageline($image_dst, $this->image_dst_x - $i - 1, $this->image_dst_y - $i - 1, $i, $this->image_dst_y - $i - 1, $c2); imageline($image_dst, $i, $i, $i, $this->image_dst_y - $i - 1, $c1); } } // add watermark image if ($this->image_watermark != '' && file_exists($this->image_watermark)) { $this->log .= '- ' . _("add watermark") . '<br />'; $this->image_watermark_position = strtolower($this->image_watermark_position); $watermark_info = getimagesize($this->image_watermark); $watermark_type = array_key_exists(2, $watermark_info) ? $watermark_info[2] : NULL; // 1 = GIF, 2 = JPG, 3 = PNG $watermark_checked = false; if ($watermark_type == 1) { if (!function_exists('imagecreatefromgif')) { $this->error = _("No create from GIF support, can't read watermark"); } else { $filter = @imagecreatefromgif($this->image_watermark); if (!$filter) { $this->error = _("No GIF read support, can't create watermark"); } else { $this->log .= ' ' . _("watermark source image is GIF") . '<br />'; $watermark_checked = true; } } } else { if ($watermark_type == 2) { if (!function_exists('imagecreatefromjpeg')) { $this->error = _("No create from JPG support, can't read watermark"); } else { $filter = @imagecreatefromjpeg($this->image_watermark); if (!$filter) { $this->error = _("No JPG read support, can't create watermark"); } else { $this->log .= ' ' . _("watermark source image is JPG") . '<br />'; $watermark_checked = true; } } } else { if ($watermark_type == 3) { if (!function_exists('imagecreatefrompng')) { $this->error = _("No create from PNG support, can't read watermark"); } else { $filter = @imagecreatefrompng($this->image_watermark); if (!$filter) { $this->error = _("No PNG read support, can't create watermark"); } else { $this->log .= ' ' . _("watermark source image is PNG") . '<br />'; $watermark_checked = true; } } } } } if ($watermark_checked) { $watermark_width = imagesx($filter); $watermark_height = imagesy($filter); $watermark_x = 0; $watermark_y = 0; if (is_numeric($this->image_watermark_x)) { if ($this->image_watermark_x < 0) { $watermark_x = $this->image_dst_x - $watermark_width + $this->image_watermark_x; } else { $watermark_x = $this->image_watermark_x; } } else { if (strpos($this->image_watermark_position, 'r') !== false) { $watermark_x = $this->image_dst_x - $watermark_width; } else { if (strpos($this->image_watermark_position, 'l') !== false) { $watermark_x = 0; } else { $watermark_x = ($this->image_dst_x - $watermark_width) / 2; } } } if (is_numeric($this->image_watermark_y)) { if ($this->image_watermark_y < 0) { $watermark_y = $this->image_dst_y - $watermark_height + $this->image_watermark_y; } else { $watermark_y = $this->image_watermark_y; } } else { if (strpos($this->image_watermark_position, 'b') !== false) { $watermark_y = $this->image_dst_y - $watermark_height; } else { if (strpos($this->image_watermark_position, 't') !== false) { $watermark_y = 0; } else { $watermark_y = ($this->image_dst_y - $watermark_height) / 2; } } } imagecopyresampled($image_dst, $filter, $watermark_x, $watermark_y, 0, 0, $watermark_width, $watermark_height, $watermark_width, $watermark_height); } else { $this->error = _("Watermark image is of unknown type"); } } // add text if (!empty($this->image_text)) { $this->log .= '- ' . _("add text") . '<br />'; if (!is_numeric($this->image_text_padding)) { $this->image_text_padding = 0; } if (!is_numeric($this->image_text_line_spacing)) { $this->image_text_line_spacing = 0; } if (!is_numeric($this->image_text_padding_x)) { $this->image_text_padding_x = $this->image_text_padding; } if (!is_numeric($this->image_text_padding_y)) { $this->image_text_padding_y = $this->image_text_padding; } $this->image_text_position = strtolower($this->image_text_position); $this->image_text_direction = strtolower($this->image_text_direction); $this->image_text_alignment = strtolower($this->image_text_alignment); // if the font is a string, we assume that we might want to load a font if (!is_numeric($this->image_text_font) && strlen($this->image_text_font) > 4 && substr(strtolower($this->image_text_font), -4) == '.gdf') { $this->log .= ' ' . _("try to load font") . ' ' . $this->image_text_font . '... '; if ($this->image_text_font = @imageloadfont($this->image_text_font)) { $this->log .= _("success") . '<br />'; } else { $this->log .= _("error") . '<br />'; $this->image_text_font = 5; } } $text = explode("\n", $this->image_text); $char_width = ImageFontWidth($this->image_text_font); $char_height = ImageFontHeight($this->image_text_font); $text_height = 0; $text_width = 0; $line_height = 0; $line_width = 0; foreach ($text as $k => $v) { if ($this->image_text_direction == 'v') { $h = $char_width * strlen($v); if ($h > $text_height) { $text_height = $h; } $line_width = $char_height; $text_width += $line_width + ($k < sizeof($text) - 1 ? $this->image_text_line_spacing : 0); } else { $w = $char_width * strlen($v); if ($w > $text_width) { $text_width = $w; } $line_height = $char_height; $text_height += $line_height + ($k < sizeof($text) - 1 ? $this->image_text_line_spacing : 0); } } $text_width += 2 * $this->image_text_padding_x; $text_height += 2 * $this->image_text_padding_y; $text_x = 0; $text_y = 0; if (is_numeric($this->image_text_x)) { if ($this->image_text_x < 0) { $text_x = $this->image_dst_x - $text_width + $this->image_text_x; } else { $text_x = $this->image_text_x; } } else { if (strpos($this->image_text_position, 'r') !== false) { $text_x = $this->image_dst_x - $text_width; } else { if (strpos($this->image_text_position, 'l') !== false) { $text_x = 0; } else { $text_x = ($this->image_dst_x - $text_width) / 2; } } } if (is_numeric($this->image_text_y)) { if ($this->image_text_y < 0) { $text_y = $this->image_dst_y - $text_height + $this->image_text_y; } else { $text_y = $this->image_text_y; } } else { if (strpos($this->image_text_position, 'b') !== false) { $text_y = $this->image_dst_y - $text_height; } else { if (strpos($this->image_text_position, 't') !== false) { $text_y = 0; } else { $text_y = ($this->image_dst_y - $text_height) / 2; } } } // add a background, maybe transparent if (!empty($this->image_text_background)) { sscanf($this->image_text_background, "#%2x%2x%2x", $red, $green, $blue); if ($gd_version >= 2 && is_numeric($this->image_text_background_percent) && $this->image_text_background_percent >= 0 && $this->image_text_background_percent <= 100) { $filter = imagecreatetruecolor($text_width, $text_height); $background_color = imagecolorallocate($filter, $red, $green, $blue); imagefilledrectangle($filter, 0, 0, $text_width, $text_height, $background_color); imagecopymerge($image_dst, $filter, $text_x, $text_y, 0, 0, $text_width, $text_height, $this->image_text_background_percent); imagedestroy($filter); } else { $background_color = imageColorAllocate($image_dst, $red, $green, $blue); imagefilledrectangle($image_dst, $text_x, $text_y, $text_x + $text_width, $text_y + $text_height, $background_color); } } $text_x += $this->image_text_padding_x; $text_y += $this->image_text_padding_y; $t_width = $text_width - 2 * $this->image_text_padding_x; $t_height = $text_height - 2 * $this->image_text_padding_y; sscanf($this->image_text_color, "#%2x%2x%2x", $red, $green, $blue); // add the text, maybe transparent if ($gd_version >= 2 && is_numeric($this->image_text_percent) && $this->image_text_percent >= 0 && $this->image_text_percent <= 100) { if ($t_width < 0) { $t_width = 0; } if ($t_height < 0) { $t_height = 0; } $filter = imagecreatetruecolor($t_width, $t_height); $color = imagecolorallocate($filter, 0, 0, $blue == 0 ? 255 : 0); imagefill($filter, 0, 0, $color); $text_color = imagecolorallocate($filter, $red, $green, $blue); imagecolortransparent($filter, $color); foreach ($text as $k => $v) { if ($this->image_text_direction == 'v') { imagestringup($filter, $this->image_text_font, $k * ($line_width + ($k > 0 && $k < sizeof($text) ? $this->image_text_line_spacing : 0)), $text_height - 2 * $this->image_text_padding_y - ($this->image_text_alignment == 'l' ? 0 : ($t_height - strlen($v) * $char_width) / ($this->image_text_alignment == 'r' ? 1 : 2)), $v, $text_color); } else { imagestring($filter, $this->image_text_font, $this->image_text_alignment == 'l' ? 0 : ($t_width - strlen($v) * $char_width) / ($this->image_text_alignment == 'r' ? 1 : 2), $k * ($line_height + ($k > 0 && $k < sizeof($text) ? $this->image_text_line_spacing : 0)), $v, $text_color); } } imagecopymerge($image_dst, $filter, $text_x, $text_y, 0, 0, $t_width, $t_height, $this->image_text_percent); imagedestroy($filter); } else { $text_color = imageColorAllocate($image_dst, $red, $green, $blue); foreach ($text as $k => $v) { if ($this->image_text_direction == 'v') { imagestringup($image_dst, $this->image_text_font, $text_x + $k * ($line_width + ($k > 0 && $k < sizeof($text) ? $this->image_text_line_spacing : 0)), $text_y + $text_height - 2 * $this->image_text_padding_y - ($this->image_text_alignment == 'l' ? 0 : ($t_height - strlen($v) * $char_width) / ($this->image_text_alignment == 'r' ? 1 : 2)), $v, $text_color); } else { imagestring($image_dst, $this->image_text_font, $text_x + ($this->image_text_alignment == 'l' ? 0 : ($t_width - strlen($v) * $char_width) / ($this->image_text_alignment == 'r' ? 1 : 2)), $text_y + $k * ($line_height + ($k > 0 && $k < sizeof($text) ? $this->image_text_line_spacing : 0)), $v, $text_color); } } } } // add a reflection if ($this->image_reflection_height) { $this->log .= '- ' . _("add reflection") . ' : ' . $this->image_reflection_height . '<br />'; // we decode image_reflection_height, which can be a integer, a string in pixels or percentage $image_reflection_height = $this->image_reflection_height; if (strpos($image_reflection_height, '%') > 0) { $image_reflection_height = $this->image_dst_y * str_replace('%', '', $image_reflection_height / 100); } if (strpos($image_reflection_height, 'px') > 0) { $image_reflection_height = str_replace('px', '', $image_reflection_height); } $image_reflection_height = (int) $image_reflection_height; if ($image_reflection_height > $this->image_dst_y) { $image_reflection_height = $this->image_dst_y; } if (empty($this->image_reflection_color)) { $this->image_reflection_color = '#FFFFFF'; } if (empty($this->image_reflection_opacity)) { $this->image_reflection_opacity = 60; } // create the new destination image $tmp = imagecreatetruecolor($this->image_dst_x, $this->image_dst_y + $image_reflection_height + $this->image_reflection_space); sscanf($this->image_reflection_color, "#%2x%2x%2x", $red, $green, $blue); $color = imagecolorallocate($tmp, $red, $green, $blue); imagefilledrectangle($tmp, 0, 0, $this->image_dst_x, $this->image_dst_y + $image_reflection_height + $this->image_reflection_space, $color); $transparency = $this->image_reflection_opacity; // copy the original image imagecopy($tmp, $image_dst, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y + ($this->image_reflection_space < 0 ? $this->image_reflection_space : 0)); // copy the reflection for ($y = 0; $y < $image_reflection_height; $y++) { imagecopymerge($tmp, $image_dst, 0, $y + $this->image_dst_y + $this->image_reflection_space, 0, $this->image_dst_y - $y - 1 + ($this->image_reflection_space < 0 ? $this->image_reflection_space : 0), $this->image_dst_x, 1, (int) $transparency); if ($transparency > 0) { $transparency = $transparency - $this->image_reflection_opacity / $image_reflection_height; } } // copy the resulting image into the destination image $this->image_dst_y = $this->image_dst_y + $image_reflection_height + $this->image_reflection_space; imagedestroy($image_dst); $image_dst = imagecreatetruecolor($this->image_dst_x, $this->image_dst_y); imagecopy($image_dst, $tmp, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y); imagedestroy($tmp); } if (is_numeric($this->jpeg_size) && $this->jpeg_size > 0 && ($this->image_convert == 'jpeg' || $this->image_convert == 'jpg')) { // inspired by: JPEGReducer class version 1, 25 November 2004, Author: Huda M ElMatsani, justhuda at netscape dot net $this->log .= '- ' . _("JPEG desired file size") . ' : ' . $this->jpeg_size . '<br />'; //calculate size of each image. 75%, 50%, and 25% quality ob_start(); imagejpeg($image_dst, '', 75); $buffer = ob_get_contents(); ob_end_clean(); $size75 = strlen($buffer); ob_start(); imagejpeg($image_dst, '', 50); $buffer = ob_get_contents(); ob_end_clean(); $size50 = strlen($buffer); ob_start(); imagejpeg($image_dst, '', 25); $buffer = ob_get_contents(); ob_end_clean(); $size25 = strlen($buffer); //calculate gradient of size reduction by quality $mgrad1 = 25 / ($size50 - $size25); $mgrad2 = 25 / ($size75 - $size50); $mgrad3 = 50 / ($size75 - $size25); $mgrad = ($mgrad1 + $mgrad2 + $mgrad3) / 3; //result of approx. quality factor for expected size $q_factor = round($mgrad * ($this->jpeg_size - $size50) + 50); if ($q_factor < 1) { $this->jpeg_quality = 1; } elseif ($q_factor > 100) { $this->jpeg_quality = 100; } else { $this->jpeg_quality = $q_factor; } $this->log .= ' ' . _("JPEG quality factor set to") . ' ' . $this->jpeg_quality . '<br />'; } // outputs image $this->log .= '- ' . _("converting..") . '<br />'; switch ($this->image_convert) { case 'jpeg': case 'jpg': $result = @imagejpeg($image_dst, $this->file_dst_pathname, $this->jpeg_quality); if (!$result) { $this->processed = false; $this->error = _("No JPEG create support"); } else { $this->log .= ' ' . _("JPEG image created") . '<br />'; } break; case 'png': $result = @imagepng($image_dst, $this->file_dst_pathname); if (!$result) { $this->processed = false; $this->error = _("No PNG create support"); } else { $this->log .= ' ' . _("PNG image created") . '<br />'; } break; case 'gif': $result = @imagegif($image_dst, $this->file_dst_pathname); if (!$result) { $this->processed = false; $this->error = _("No GIF create support"); } else { $this->log .= ' ' . _("GIF image created") . '<br />'; } break; default: $this->processed = false; $this->error = _("No convertion type defined"); } if ($this->processed) { if (is_resource($image_src)) { imagedestroy($image_src); } if (is_resource($image_dst)) { imagedestroy($image_dst); } $this->log .= ' ' . _("image objects destroyed") . '<br />'; } } if ($has_temp_file) { $this->log .= '- ' . _("deletes temporary file") . '<br />'; @unlink($this->file_src_pathname); } } else { $this->log .= '- ' . _("no image processing wanted") . '<br />'; if (!$this->no_upload_check) { $result = is_uploaded_file($this->file_src_pathname); } else { $result = TRUE; } if ($result) { if (!$this->no_upload_check) { if (!move_uploaded_file($this->file_src_pathname, $this->file_dst_pathname)) { $this->processed = false; $this->error = _("Error copying file on the server. move_uploaded_file() failed"); } } else { if (!copy($this->file_src_pathname, $this->file_dst_pathname)) { $this->processed = false; $this->error = _("Error copying file on the server. copy() failed"); } } } else { $this->processed = false; $this->error = _("Error copying file on the server. Incorrect source file"); } } } if ($this->processed) { $this->log .= '- <b>' . _("process OK") . '</b><br />'; } // we reinit all the var $this->init(); }
/** * Draw the CAPTCHA code over the image * * @access private * */ function drawWord() { if ($this->use_gd_font == true) { if (!is_int($this->gd_font_file)) { //is a file name $font = @imageloadfont($this->gd_font_file); if ($font == false) { trigger_error("Failed to load GD Font file {$this->gd_font_file} ", E_USER_WARNING); return; } } else { //gd font identifier $font = $this->gd_font_file; } $color = imagecolorallocate($this->im, hexdec(substr($this->text_color, 1, 2)), hexdec(substr($this->text_color, 3, 2)), hexdec(substr($this->text_color, 5, 2))); imagestring($this->im, $font, $this->text_x_start, $this->image_height / 2 - $this->gd_font_size / 2, $this->code, $color); } else { //ttf font if ($this->use_transparent_text == true) { $alpha = intval($this->text_transparency_percentage / 100 * 127); $font_color = imagecolorallocatealpha($this->im, hexdec(substr($this->text_color, 1, 2)), hexdec(substr($this->text_color, 3, 2)), hexdec(substr($this->text_color, 5, 2)), $alpha); } else { //no transparency $font_color = imagecolorallocate($this->im, hexdec(substr($this->text_color, 1, 2)), hexdec(substr($this->text_color, 3, 2)), hexdec(substr($this->text_color, 5, 2))); } $x = $this->text_x_start; $strlen = strlen($this->code); $y_min = $this->image_height / 2 + $this->font_size / 2 - 2; $y_max = $this->image_height / 2 + $this->font_size / 2 + 2; $colors = explode(',', $this->multi_text_color); for ($i = 0; $i < $strlen; ++$i) { $angle = rand($this->text_angle_minimum, $this->text_angle_maximum); $y = rand($y_min, $y_max); if ($this->use_multi_text == true) { $idx = rand(0, sizeof($colors) - 1); $r = substr($colors[$idx], 1, 2); $g = substr($colors[$idx], 3, 2); $b = substr($colors[$idx], 5, 2); if ($this->use_transparent_text == true) { $font_color = imagecolorallocatealpha($this->im, "0x{$r}", "0x{$g}", "0x{$b}", $alpha); } else { $font_color = imagecolorallocate($this->im, "0x{$r}", "0x{$g}", "0x{$b}"); } } @imagettftext($this->im, $this->font_size, $angle, $x, $y, $font_color, $this->ttf_file, $this->code[$i]); $x += rand($this->text_minimum_distance, $this->text_maximum_distance); } //for loop } //else ttf font }
/** * Draw the CAPTCHA code over the image * * @access private * */ function drawWord() { if ($this->use_gd_font == true) { if (!is_int($this->gd_font_file)) { //is a file name $font = @imageloadfont($this->gd_font_file); if ($font == false) { trigger_error("Failed to load GD Font file {$this->gd_font_file} ", E_USER_WARNING); return; } } else { //gd font identifier $font = $this->gd_font_file; } $color = imagecolorallocate($this->im, hexdec(substr($this->text_color, 1, 2)), hexdec(substr($this->text_color, 3, 2)), hexdec(substr($this->text_color, 5, 2))); imagestring($this->im, $font, $this->text_x_start, $this->image_height / 2 - $this->gd_font_size / 2, $this->code, $color); } else { //ttf font if ($this->use_transparent_text == true) { $alpha = intval($this->text_transparency_percentage / 100 * 127); $font_color = imagecolorallocatealpha($this->im, hexdec(substr($this->text_color, 1, 2)), hexdec(substr($this->text_color, 3, 2)), hexdec(substr($this->text_color, 5, 2)), $alpha); } else { //no transparency $font_color = imagecolorallocate($this->im, hexdec(substr($this->text_color, 1, 2)), hexdec(substr($this->text_color, 3, 2)), hexdec(substr($this->text_color, 5, 2))); } $x = $this->text_x_start; $strlen = strlen($this->code); $y_min = $this->image_height / 2 + $this->font_size / 2 - 2; $y_max = $this->image_height / 2 + $this->font_size / 2 + 2; $colors = explode(',', $this->multi_text_color); for ($i = 0; $i < $strlen; ++$i) { $angle = rand($this->text_angle_minimum, $this->text_angle_maximum); $y = rand($y_min, $y_max); if ($this->use_multi_text == true) { $idx = rand(0, sizeof($colors) - 1); $r = substr($colors[$idx], 1, 2); $g = substr($colors[$idx], 3, 2); $b = substr($colors[$idx], 5, 2); if ($this->use_transparent_text == true) { $font_color = imagecolorallocatealpha($this->im, "0x{$r}", "0x{$g}", "0x{$b}", $alpha); } else { $font_color = imagecolorallocate($this->im, "0x{$r}", "0x{$g}", "0x{$b}"); } } /* Will check if the FreeType library is loaded, if not, use the default PHP fonts. * @customized by ATutor, Harris Wong */ if (function_exists('imagettftext')) { @imagettftext($this->im, $this->font_size, $angle, $x, $y, $font_color, $this->ttf_file, $this->code[$i]); } else { $this->arc_linethrough = false; //no arcline then,too hard to see with the built in fonts and limited size imagestring($this->im, 5, $x, $y - 15, $this->code[$i], $font_color); } $x += rand($this->text_minimum_distance, $this->text_maximum_distance); } //for loop } //else ttf font }
function showCodeImage($code) { global $settings, $user_info, $modSettings; /* Note: The higher the value of visual_verification_type the harder the verification is - from 0 as disabled through to 4 as "Very hard". */ // What type are we going to be doing? $imageType = $modSettings['visual_verification_type']; // Special case to allow the admin center to show samples. if ($user_info['is_admin'] && isset($_GET['type'])) { $imageType = (int) $_GET['type']; } // Some quick references for what we do. // Do we show no, low or high noise? $noiseType = $imageType == 3 ? 'low' : ($imageType == 4 ? 'high' : ($imageType == 5 ? 'extreme' : 'none')); // Can we have more than one font in use? $varyFonts = $imageType > 3 ? true : false; // Just a plain white background? $simpleBGColor = $imageType < 3 ? true : false; // Plain black foreground? $simpleFGColor = $imageType == 0 ? true : false; // High much to rotate each character. $rotationType = $imageType == 1 ? 'none' : ($imageType > 3 ? 'low' : 'high'); // Do we show some characters inversed? $showReverseChars = $imageType > 3 ? true : false; // Special case for not showing any characters. $disableChars = $imageType == 0 ? true : false; // What do we do with the font colors. Are they one color, close to one color or random? $fontColorType = $imageType == 1 ? 'plain' : ($imageType > 3 ? 'random' : 'cyclic'); // Are the fonts random sizes? $fontSizeRandom = $imageType > 3 ? true : false; // How much space between characters? $fontHorSpace = $imageType > 3 ? 'high' : ($imageType == 1 ? 'medium' : 'minus'); // Where do characters sit on the image? (Fixed position or random/very random) $fontVerPos = $imageType == 1 ? 'fixed' : ($imageType > 3 ? 'vrandom' : 'random'); // Make font semi-transparent? $fontTrans = $imageType == 2 || $imageType == 3 ? true : false; // Give the image a border? $hasBorder = $simpleBGColor; // Is this GD2? Needed for pixel size. $testGD = get_extension_funcs('gd'); $gd2 = in_array('imagecreatetruecolor', $testGD) && function_exists('imagecreatetruecolor'); unset($testGD); // The amount of pixels inbetween characters. $character_spacing = 1; // What color is the background - generally white unless we're on "hard". if ($simpleBGColor) { $background_color = array(255, 255, 255); } else { $background_color = isset($settings['verification_background']) ? $settings['verification_background'] : array(236, 237, 243); } // The color of the characters shown (red, green, blue). if ($simpleFGColor) { $foreground_color = array(0, 0, 0); } else { $foreground_color = array(64, 101, 136); // Has the theme author requested a custom color? if (isset($settings['verification_foreground'])) { $foreground_color = $settings['verification_foreground']; } } if (!is_dir($settings['default_theme_dir'] . '/fonts')) { return false; } // Get a list of the available fonts. $font_dir = dir($settings['default_theme_dir'] . '/fonts'); $font_list = array(); $ttfont_list = array(); while ($entry = $font_dir->read()) { if (preg_match('~^(.+)\\.gdf$~', $entry, $matches) === 1) { $font_list[] = $entry; } elseif (preg_match('~^(.+)\\.ttf$~', $entry, $matches) === 1) { $ttfont_list[] = $entry; } } if (empty($font_list)) { return false; } // For non-hard things don't even change fonts. if (!$varyFonts) { $font_list = array($font_list[0]); // Try use Screenge if we can - it looks good! if (in_array('Screenge.ttf', $ttfont_list)) { $ttfont_list = array('Screenge.ttf'); } else { $ttfont_list = empty($ttfont_list) ? array() : array($ttfont_list[0]); } } // Create a list of characters to be shown. $characters = array(); $loaded_fonts = array(); for ($i = 0; $i < strlen($code); $i++) { $characters[$i] = array('id' => $code[$i], 'font' => array_rand($font_list)); $loaded_fonts[$characters[$i]['font']] = null; } // Load all fonts and determine the maximum font height. foreach ($loaded_fonts as $font_index => $dummy) { $loaded_fonts[$font_index] = imageloadfont($settings['default_theme_dir'] . '/fonts/' . $font_list[$font_index]); } // Determine the dimensions of each character. $total_width = $character_spacing * strlen($code) + 20; $max_height = 0; foreach ($characters as $char_index => $character) { $characters[$char_index]['width'] = imagefontwidth($loaded_fonts[$character['font']]); $characters[$char_index]['height'] = imagefontheight($loaded_fonts[$character['font']]); $max_height = max($characters[$char_index]['height'] + 5, $max_height); $total_width += $characters[$char_index]['width']; } // Create an image. $code_image = $gd2 ? imagecreatetruecolor($total_width, $max_height) : imagecreate($total_width, $max_height); // Draw the background. $bg_color = imagecolorallocate($code_image, $background_color[0], $background_color[1], $background_color[2]); imagefilledrectangle($code_image, 0, 0, $total_width - 1, $max_height - 1, $bg_color); // Randomize the foreground color a little. for ($i = 0; $i < 3; $i++) { $foreground_color[$i] = mt_rand(max($foreground_color[$i] - 3, 0), min($foreground_color[$i] + 3, 255)); } $fg_color = imagecolorallocate($code_image, $foreground_color[0], $foreground_color[1], $foreground_color[2]); // Color for the dots. for ($i = 0; $i < 3; $i++) { $dotbgcolor[$i] = $background_color[$i] < $foreground_color[$i] ? mt_rand(0, max($foreground_color[$i] - 20, 0)) : mt_rand(min($foreground_color[$i] + 20, 255), 255); } $randomness_color = imagecolorallocate($code_image, $dotbgcolor[0], $dotbgcolor[1], $dotbgcolor[2]); // Some squares/rectanges for new extreme level if ($noiseType == 'extreme') { for ($i = 0; $i < rand(1, 5); $i++) { $x1 = rand(0, $total_width / 4); $x2 = $x1 + round(rand($total_width / 4, $total_width)); $y1 = rand(0, $max_height); $y2 = $y1 + round(rand(0, $max_height / 3)); imagefilledrectangle($code_image, $x1, $y1, $x2, $y2, mt_rand(0, 1) ? $fg_color : $randomness_color); } } // Fill in the characters. if (!$disableChars) { $cur_x = 0; foreach ($characters as $char_index => $character) { // Can we use true type fonts? $can_do_ttf = function_exists('imagettftext'); // How much rotation will we give? if ($rotationType == 'none') { $angle = 0; } else { $angle = mt_rand(-100, 100) / ($rotationType == 'high' ? 6 : 10); } // What color shall we do it? if ($fontColorType == 'cyclic') { // Here we'll pick from a set of acceptance types. $colors = array(array(10, 120, 95), array(46, 81, 29), array(4, 22, 154), array(131, 9, 130), array(0, 0, 0), array(143, 39, 31)); if (!isset($last_index)) { $last_index = -1; } $new_index = $last_index; while ($last_index == $new_index) { $new_index = mt_rand(0, count($colors) - 1); } $char_fg_color = $colors[$new_index]; $last_index = $new_index; } elseif ($fontColorType == 'random') { $char_fg_color = array(mt_rand(max($foreground_color[0] - 2, 0), $foreground_color[0]), mt_rand(max($foreground_color[1] - 2, 0), $foreground_color[1]), mt_rand(max($foreground_color[2] - 2, 0), $foreground_color[2])); } else { $char_fg_color = array($foreground_color[0], $foreground_color[1], $foreground_color[2]); } if (!empty($can_do_ttf)) { // GD2 handles font size differently. if ($fontSizeRandom) { $font_size = $gd2 ? mt_rand(17, 19) : mt_rand(18, 25); } else { $font_size = $gd2 ? 18 : 24; } // Work out the sizes - also fix the character width cause TTF not quite so wide! $font_x = $fontHorSpace == 'minus' && $cur_x > 0 ? $cur_x - 3 : $cur_x + 5; $font_y = $max_height - ($fontVerPos == 'vrandom' ? mt_rand(2, 8) : ($fontVerPos == 'random' ? mt_rand(3, 5) : 5)); // What font face? if (!empty($ttfont_list)) { $fontface = $settings['default_theme_dir'] . '/fonts/' . $ttfont_list[mt_rand(0, count($ttfont_list) - 1)]; } // What color are we to do it in? $is_reverse = $showReverseChars ? mt_rand(0, 1) : false; $char_color = function_exists('imagecolorallocatealpha') && $fontTrans ? imagecolorallocatealpha($code_image, $char_fg_color[0], $char_fg_color[1], $char_fg_color[2], 50) : imagecolorallocate($code_image, $char_fg_color[0], $char_fg_color[1], $char_fg_color[2]); $fontcord = @imagettftext($code_image, $font_size, $angle, $font_x, $font_y, $char_color, $fontface, $character['id']); if (empty($fontcord)) { $can_do_ttf = false; } elseif ($is_reverse) { imagefilledpolygon($code_image, $fontcord, 4, $fg_color); // Put the character back! imagettftext($code_image, $font_size, $angle, $font_x, $font_y, $randomness_color, $fontface, $character['id']); } if ($can_do_ttf) { $cur_x = max($fontcord[2], $fontcord[4]) + ($angle == 0 ? 0 : 3); } } if (!$can_do_ttf) { // Rotating the characters a little... if (function_exists('imagerotate')) { $char_image = $gd2 ? imagecreatetruecolor($character['width'], $character['height']) : imagecreate($character['width'], $character['height']); $char_bgcolor = imagecolorallocate($char_image, $background_color[0], $background_color[1], $background_color[2]); imagefilledrectangle($char_image, 0, 0, $character['width'] - 1, $character['height'] - 1, $char_bgcolor); imagechar($char_image, $loaded_fonts[$character['font']], 0, 0, $character['id'], imagecolorallocate($char_image, $char_fg_color[0], $char_fg_color[1], $char_fg_color[2])); $rotated_char = imagerotate($char_image, mt_rand(-100, 100) / 10, $char_bgcolor); imagecopy($code_image, $rotated_char, $cur_x, 0, 0, 0, $character['width'], $character['height']); imagedestroy($rotated_char); imagedestroy($char_image); } else { imagechar($code_image, $loaded_fonts[$character['font']], $cur_x, floor(($max_height - $character['height']) / 2), $character['id'], imagecolorallocate($code_image, $char_fg_color[0], $char_fg_color[1], $char_fg_color[2])); } $cur_x += $character['width'] + $character_spacing; } } } else { imageline($code_image, 0, 0, $total_width, $max_height, $fg_color); imageline($code_image, 0, $max_height, $total_width, 0, $fg_color); } // Make the background color transparent on the hard image. if (!$simpleBGColor) { imagecolortransparent($code_image, $bg_color); } if ($hasBorder) { imagerectangle($code_image, 0, 0, $total_width - 1, $max_height - 1, $fg_color); } // Add some noise to the background? if ($noiseType != 'none') { for ($i = mt_rand(0, 2); $i < $max_height; $i += mt_rand(1, 2)) { for ($j = mt_rand(0, 10); $j < $total_width; $j += mt_rand(1, 10)) { imagesetpixel($code_image, $j, $i, mt_rand(0, 1) ? $fg_color : $randomness_color); } } // Put in some lines too? if ($noiseType != 'extreme') { $num_lines = $noiseType == 'high' ? mt_rand(3, 7) : mt_rand(2, 5); for ($i = 0; $i < $num_lines; $i++) { if (mt_rand(0, 1)) { $x1 = mt_rand(0, $total_width); $x2 = mt_rand(0, $total_width); $y1 = 0; $y2 = $max_height; } else { $y1 = mt_rand(0, $max_height); $y2 = mt_rand(0, $max_height); $x1 = 0; $x2 = $total_width; } imagesetthickness($code_image, mt_rand(1, 2)); imageline($code_image, $x1, $y1, $x2, $y2, mt_rand(0, 1) ? $fg_color : $randomness_color); } } else { // Put in some ellipse $num_ellipse = $noiseType == 'extreme' ? mt_rand(6, 12) : mt_rand(2, 6); for ($i = 0; $i < $num_ellipse; $i++) { $x1 = round(rand($total_width / 4 * -1, $total_width + $total_width / 4)); $x2 = round(rand($total_width / 2, 2 * $total_width)); $y1 = round(rand($max_height / 4 * -1, $max_height + $max_height / 4)); $y2 = round(rand($max_height / 2, 2 * $max_height)); imageellipse($code_image, $x1, $y1, $x2, $y2, mt_rand(0, 1) ? $fg_color : $randomness_color); } } } // Show the image. if (function_exists('imagegif')) { header('Content-type: image/gif'); imagegif($code_image); } else { header('Content-type: image/png'); imagepng($code_image); } // Bail out. imagedestroy($code_image); die; }
function marktpreise() { // berechnet die marktpreise neu $select = "SELECT SUM( eisenmine ) AS ges FROM `gebauede`;"; $result = mysql_query($select); $row = mysql_fetch_array($result); $eisen_gw = ($row['ges'] * 30 / 100 * ($row['ges'] * 5) + (40 + $row['ges'] * 30)) * 2; $select = "SELECT SUM( titanmine ) AS ges FROM `gebauede`;"; $result = mysql_query($select); $row = mysql_fetch_array($result); $titan_gw = ($row['ges'] * 20 / 100 * ($row['ges'] * 5) + (20 + $row['ges'] * 20)) * 2; $select = "SELECT SUM( oelpumpe ) AS ges FROM `gebauede`;"; $result = mysql_query($select); $row = mysql_fetch_array($result); $oel_gw = ($row['ges'] * 25 / 100 * ($row['ges'] * 5) + (32 + $row['ges'] * 25)) * 2; $select = "SELECT SUM( uranmine ) AS ges FROM `gebauede`;"; $result = mysql_query($select); $row = mysql_fetch_array($result); $uran_gw = ($row['ges'] * 12 / 100 * ($row['ges'] * 5) + $row['ges'] * 12) * 2; $select = "SELECT einheit, SUM(menge) as ges FROM `markt` WHERE `type` = '0' AND `einheit` = '1' GROUP BY einheit;"; $result = mysql_query($select); $row = mysql_fetch_array($result); $ges['eisen'] = $row['ges'] + $eisen_gw; $select = "SELECT einheit, SUM(menge) as ges FROM `markt` WHERE `type` = '0' AND `einheit` = '2' GROUP BY einheit;"; $result = mysql_query($select); $row = mysql_fetch_array($result); $ges['titan'] = $row['ges'] + $titan_gw; $select = "SELECT einheit, SUM(menge) as ges FROM `markt` WHERE `type` = '0' AND `einheit` = '3' GROUP BY einheit;"; $result = mysql_query($select); $row = mysql_fetch_array($result); $ges['oel'] = $row['ges'] + $oel_gw; $select = "SELECT einheit, SUM(menge) as ges FROM `markt` WHERE `type` = '0' AND `einheit` = '4' GROUP BY einheit;"; $result = mysql_query($select); $row = mysql_fetch_array($result); $ges['uran'] = $row['ges'] + $uran_gw; $select = "SELECT einheit, SUM(menge) as ges FROM `markt` WHERE `type` = '1' AND `einheit` = '1' GROUP BY einheit;"; $result = mysql_query($select); $row = mysql_fetch_array($result); $mges['eisen'] = $row['ges'] + $eisen_gw; $select = "SELECT einheit, SUM(menge) as ges FROM `markt` WHERE `type` = '1' AND `einheit` = '2' GROUP BY einheit;"; $result = mysql_query($select); $row = mysql_fetch_array($result); $mges['titan'] = $row['ges'] + $titan_gw; $select = "SELECT einheit, SUM(menge) as ges FROM `markt` WHERE `type` = '1' AND `einheit` = '3' GROUP BY einheit;"; $result = mysql_query($select); $row = mysql_fetch_array($result); $mges['oel'] = $row['ges'] + $oel_gw; $select = "SELECT einheit, SUM(menge) as ges FROM `markt` WHERE `type` = '1' AND `einheit` = '4' GROUP BY einheit;"; $result = mysql_query($select); $row = mysql_fetch_array($result); $mges['uran'] = $row['ges'] + $uran_gw; $ek['eisen'] = number_format(300 / 100 * $ges['eisen'] / (($ges['eisen'] + $mges['eisen']) / 100), 2); $vk['eisen'] = number_format(300 / 100 * $ges['eisen'] / (($ges['eisen'] + $mges['eisen']) / 100) / 2, 2); $ek['titan'] = number_format(400 / 100 * $ges['titan'] / (($ges['titan'] + $mges['titan']) / 100), 2); $vk['titan'] = number_format(400 / 100 * $ges['titan'] / (($ges['titan'] + $mges['titan']) / 100) / 2, 2); $ek['oel'] = number_format(350 / 100 * $ges['oel'] / (($ges['oel'] + $mges['oel']) / 100), 2); $vk['oel'] = number_format(350 / 100 * $ges['oel'] / (($ges['oel'] + $mges['oel']) / 100) / 2, 2); $ek['uran'] = number_format(500 / 100 * $ges['uran'] / (($ges['uran'] + $mges['uran']) / 100), 2); $vk['uran'] = number_format(500 / 100 * $ges['uran'] / (($ges['uran'] + $mges['uran']) / 100) / 2, 2); mysql_query("INSERT INTO `marktpreise` ( `id` , `time` , `ek_eisen` , `ek_oel` , `ek_titan` , `ek_uran` , `vk_eisen` , `vk_oel` , `vk_titan` , `vk_uran` ) VALUES ( '', '" . date('U') . "', '" . $ek['eisen'] . "', '" . $ek['oel'] . "', '" . $ek['titan'] . "', '" . $ek['uran'] . "', '" . $vk['eisen'] . "', '" . $vk['oel'] . "', '" . $vk['titan'] . "', '" . $vk['uran'] . "' );"); $bild1 = imagecreatefromgif("img/graph.gif"); $bild2 = imagecreatefromgif("img/graph.gif"); $bild3 = imagecreatefromgif("img/graph.gif"); $bild4 = imagecreatefromgif("img/graph.gif"); $farbe = imagecolorallocate($bild1, 0, 0, 0); $font = imageloadfont('code/addlg10.gdf'); imagestring($bild1, 10, 30, 230, 'Preisentwicklung Eisen ' . date('(d.m.Y - H:i)'), $farbe); imagestring($bild2, 10, 30, 230, 'Preisentwicklung Titan ' . date('(d.m.Y - H:i)'), $farbe); imagestring($bild3, 10, 40, 230, 'Preisentwicklung Oel ' . date('(d.m.Y - H:i)'), $farbe); imagestring($bild4, 10, 35, 230, 'Preisentwicklung Uran ' . date('(d.m.Y - H:i)'), $farbe); $result = mysql_query("SELECT * FROM `marktpreise` GROUP BY `id` DESC LIMIT 0,42;"); do { $i++; $row = mysql_fetch_array($result); $y1 = number_format($row['ek_eisen'] / 2, 0, '', ''); $y2 = number_format($row['ek_titan'] / 2, 0, '', ''); $y3 = number_format($row['ek_oel'] / 2, 0, '', ''); $y4 = number_format($row['ek_uran'] / 2, 0, '', ''); if ($oldy1 != '') { imageline($bild1, 420 - $i * 10, 210 - $y1, 420 - ($i - 1) * 10, 210 - $oldy1, $farbe); imageline($bild2, 420 - $i * 10, 210 - $y2, 420 - ($i - 1) * 10, 210 - $oldy2, $farbe); imageline($bild3, 420 - $i * 10, 210 - $y3, 420 - ($i - 1) * 10, 210 - $oldy3, $farbe); imageline($bild4, 420 - $i * 10, 210 - $y4, 420 - ($i - 1) * 10, 210 - $oldy4, $farbe); } $oldy1 = $y1; $oldy2 = $y2; $oldy3 = $y3; $oldy4 = $y4; } while ($row and $i < 41); imagepng($bild1, 'eisenstats.png'); imagepng($bild2, 'titanstats.png'); imagepng($bild3, 'oelstats.png'); imagepng($bild4, 'uranstats.png'); }
{ return array(hexdec(substr($color, 0, 2)), hexdec(substr($color, 2, 2)), hexdec(substr($color, 4, 2))); } for ($i = 1; $i <= 3; $i++) { if (is_array($var['line' . $i]['value_p'])) { $use_country = true; $_flag_xy = $flag_xy[$i]; $_flag = $var['line' . $i]['value_p']['iso']; $var['line' . $i]['value_p'] = $var['line' . $i]['value_p']['name']; } } //create image $im = imagecreatetruecolor(150, 190); //load font $fonts = array(1 => 'msmincho.gdf', 2 => 'smallfont.gdf', 3 => 'visitort2.gdf'); $font = imageloadfont("fonts/" . $fonts[$var['font']]); //define colors //border color list($br, $bg, $bb) = hex2rgb($var['bColor']); $bColor = imagecolorallocate($im, $br, $bg, $bb); //background color list($bgr, $bgg, $bgb) = hex2rgb($var['bgColor']); $bgColor = imagecolorallocate($im, $bgr, $bgg, $bgb); //font color list($fr, $fg, $fb) = hex2rgb($var['fontColor']); $fontColor = imagecolorallocate($im, $fr, $fg, $fb); //fill avatar body with the background color imagefilledrectangle($im, 0, 0, 150, 190, $bgColor); //draw border imagerectangle($im, 0, 0, 149, 189, $bColor); //add smile
<?php session_start(); header("Content-type: image/png"); $captcha_image = imagecreatefrompng("counter/captcha.png"); $captcha_font = imageloadfont("counter/font.gdf"); $captcha_text = substr(md5(uniqid('')), -6, 6); $_SESSION['captcha_session'] = $captcha_text; $captcha_color = imagecolorallocate($captcha_image, 0, 0, 0); imagestring($captcha_image, $captcha_font, 15, 5, $captcha_text, $captcha_color); imagepng($captcha_image); imagedestroy($captcha_image);
public function render($params = array()) { $roit1 = rand(1, 6); $font = dirname(__FILE__) . DS . 'catcha_fonts' . DS . 'font' . $roit1 . '.ttf'; $font = normalize_path($font, 0); if (function_exists('imagettftext')) { $text1 = mt_rand(100, 4500); } else { $text1 = mt_rand(100, 999); } $text2 = mt_rand(2, 9); $roit = mt_rand(1, 5); $text = "{$text1}"; $answ = $text1; $x = 100; $y = 20; $image = @imagecreate($x, 20) or die('Unable to render a CAPTCHA picture!'); $tcol1z = rand(1, 150); $ttcol1z1 = rand(0, 150); $tcol1z11 = rand(0, 150); $bgcolor = imagecolorallocate($image, 255, 255, 255); $gray = imagecolorallocate($image, 240, 240, 240); // $black = imagecolorallocate($image, $tcol1z, $ttcol1z1, $tcol1z11); $black = imagecolorallocate($image, 0, 0, 0); $captcha_sid = 'captcha'; if (isset($params['id'])) { $captcha_sid = 'captcha_' . $params['id']; } elseif (isset($_GET['id'])) { $captcha_sid = 'captcha_' . $_GET['id']; } $old = mw()->user_manager->session_get('captcha'); if ($old != false) { $old_array = mw()->user_manager->session_get('captcha_recent'); if (!is_array($old_array)) { $old_array = array(); } array_unshift($old_array, $old); array_slice($old_array, 20); mw()->user_manager->session_set('captcha_recent', $old_array); } $sess = mw()->user_manager->session_set($captcha_sid, $answ); $col1z = rand(200, 242); $col1z1 = rand(150, 242); $col1z11 = rand(150, 242); $color1 = imagecolorallocate($image, $col1z, $col1z1, $tcol1z11); $color2 = imagecolorallocate($image, $tcol1z - 1, $ttcol1z1 - 1, $tcol1z11 - 2); // imagefill($image, 0, 0, $color1); for ($i = 0; $i < $x; ++$i) { for ($j = 0; $j < $y; ++$j) { if (mt_rand(0, 20) < 10) { //$coords = array(mt_rand(0, 10), mt_rand(0, 10), mt_rand(0, 10), mt_rand(0, 10), 5, 6); $y21 = mt_rand(5, 20); $this->captcha_vector($image, $x - mt_rand(0, 10), mt_rand(0, 10), mt_rand(0, 180), 200, $bgcolor); // imagesetpixel($image, $i, $j, $color2); } } } $x1 = mt_rand(0, 5); $y1 = mt_rand(20, 22); $tsize = rand(13, 15); $pad = 2; // extra char spacing for text if (function_exists('imagettftext')) { imagettftext($image, $tsize, $roit, $x1, $y1, $black, $font, $text); } else { if (function_exists('imagestring')) { $font = mw_includes_path() . DS . 'admin' . DS . 'catcha_fonts' . DS . 'font' . $roit1 . '.gdf'; $font = normalize_path($font, 0); $font = imageloadfont($font); imagestring($image, $font, 0, 0, $text, $black); } else { } } $s = 180; $e = 360; if (function_exists('imagefilter')) { $filter_img = rand(1, 6); switch ($filter_img) { case 1: $gaussian = array(array(1.0, 2.0, 1.0), array(2.0, 4.0, 2.0), array(1.0, 2.0, 1.0)); imageconvolution($image, $gaussian, 16, 0); break; // break; // break; case 3: imagefilter($image, IMG_FILTER_PIXELATE, 1); break; default: break; } } $y21 = mt_rand(5, 20); $this->captcha_vector($image, $x, $y21 / 2, 180, 200, $bgcolor); $y21 = mt_rand(5, 20); $this->captcha_vector($image, $x, $y21 / 2, $col1z11, 200, $bgcolor); $y21 = mt_rand(5, 20); $this->captcha_vector($image, $x / 3, $y21 / 3, $col1z11, 200, $bgcolor); if (function_exists('imagestring')) { $this->captcha_vector($image, $x / 3, $y21 / 3, $col1z11, 200, $gray); imagestring($image, 0, $y21, 2, $text, $gray); } // $emboss = array(array(2, 0, 0), array(0, -1, 0), array(0, 0, -1)); // $embize = mt_rand(1, 4); header('Content-type: image/png'); header('Cache-Control: no-store, no-cache, must-revalidate'); header('Cache-Control: post-check=0, pre-check=0', false); header('Pragma: no-cache'); imagepng($image); imagecolordeallocate($image, $bgcolor); imagecolordeallocate($image, $black); imagedestroy($image); }
/** * Draw the CAPTCHA code over the image * * @access private * */ function drawWord() { $width2 = $this->image_width * $this->iscale; $height2 = $this->image_height * $this->iscale; if ($this->use_gd_font == true || !is_readable($this->ttf_file)) { if (!is_int($this->gd_font_file)) { //is a file name $font = @imageloadfont($this->gd_font_file); if ($font == false) { trigger_error("Failed to load GD Font file {$this->gd_font_file} ", E_USER_WARNING); return; } } else { //gd font identifier $font = $this->gd_font_file; } imagestring($this->im, $font, $this->text_x_start, $this->image_height / 2 - $this->gd_font_size / 2, $this->code, $this->gdtextcolor); } else { //ttf font $font_size = $height2 * 0.35; $bb = imagettfbbox($font_size, 0, $this->ttf_file, $this->code); $tx = $bb[4] - $bb[0]; $ty = $bb[5] - $bb[1]; $x = floor($width2 / 2 - $tx / 2 - $bb[0]); $y = round($height2 / 2 - $ty / 2 - $bb[1]); $strlen = strlen($this->code); if (!is_array($this->multi_text_color)) { $this->use_multi_text = false; } if ($this->use_multi_text == false && $this->text_angle_minimum == 0 && $this->text_angle_maximum == 0) { // no angled or multi-color characters imagettftext($this->tmpimg, $font_size, 0, $x, $y, $this->gdtextcolor, $this->ttf_file, $this->code); } else { for ($i = 0; $i < $strlen; ++$i) { $angle = rand($this->text_angle_minimum, $this->text_angle_maximum); $y = rand($y - 5, $y + 5); if ($this->use_multi_text == true) { $font_color = $this->gdmulticolor[rand(0, sizeof($this->gdmulticolor) - 1)]; } else { $font_color = $this->gdtextcolor; } $ch = $this->code[$i]; imagettftext($this->tmpimg, $font_size, $angle, $x, $y, $font_color, $this->ttf_file, $ch); // estimate character widths to increment $x without creating spaces that are too large or too small // these are best estimates to align text but may vary between fonts // for optimal character widths, do not use multiple text colors or character angles and the complete string will be written by imagettftext if (strpos('abcdeghknopqsuvxyz', $ch) !== false) { $min_x = $font_size - $this->iscale * 6; $max_x = $font_size - $this->iscale * 6; } else { if (strpos('ilI1', $ch) !== false) { $min_x = $font_size / 5; $max_x = $font_size / 3; } else { if (strpos('fjrt', $ch) !== false) { $min_x = $font_size - $this->iscale * 12; $max_x = $font_size - $this->iscale * 12; } else { if ($ch == 'wm') { $min_x = $font_size; $max_x = $font_size + $this->iscale * 3; } else { // numbers, capitals or unicode $min_x = $font_size + $this->iscale * 2; $max_x = $font_size + $this->iscale * 5; } } } } $x += rand($min_x, $max_x); } //for loop } // angled or multi-color } //else ttf font //$this->im = $this->tmpimg; //$this->output(); }
/** * 生成验证码图像,返回 QView_Output 对象 * * $param string $code * * @return QView_Output */ function generateImage($code) { // 根据选项确定绘图选项 $padding = intval($this->_options['padding']); if ($padding < 0) { $padding = 0; } $color = $this->_options['color']; $bgcolor = $this->_options['bgcolor']; $border = $this->_options['border']; $bdcolor = $this->_options['bdcolor']; $float_pixel = intval($this->_options['float_pixel']); // 确定要使用的字体 if (is_int($this->_options['font'])) { $font = intval($this->_options['font']); if ($font < 0 || $font > 5) { $font = 5; } } else { $font = imageloadfont($this->_options['font']); } // 确定字体宽度和高度 $font_width = imagefontwidth($font); $font_height = imagefontheight($font); // 确定图像的宽度和高度 $width = $font_width * strlen($code) + $padding * 2 + $border * 2 + 1; $height = $font_height + $padding * 2 + $border * 2 + 1 + $float_pixel; // 创建图像 $img = imagecreate($width, $height); // 绘制边框 if ($border) { list($r, $g, $b) = Helper_ImgCode::hex2rgb($bdcolor); $color = imagecolorallocate($img, $r, $g, $b); imagefilledrectangle($img, 0, 0, $width, $height, $color); } // 绘制背景 list($r, $g, $b) = Helper_ImgCode::hex2rgb($bgcolor); $color = imagecolorallocate($img, $r, $g, $b); imagefilledrectangle($img, $border, $border, $width - $border - 1, $height - $border - 1, $color); // 绘制文字 list($r, $g, $b) = Helper_ImgCode::hex2rgb($color); $color = imagecolorallocate($img, $r, $g, $b); for ($i = 0, $max = strlen($code); $i < $max; $i++) { imagestring($img, $font, $padding + $border + $font_width * $i, $padding + $border + mt_rand(0, $float_pixel), $code[$i], $color); } $filename = 'imgcode-' . mt_rand(); ob_start(); // 输出图像 switch (strtolower($this->_options['image_type'])) { case 'png': $filename .= '.png'; $mime = image_type_to_mime_type(IMAGETYPE_PNG); imagepng($img); break; case 'gif': $filename .= '.gif'; $mime = image_type_to_mime_type(IMAGETYPE_GIF); imagegif($img); break; case 'jpg': default: $filename .= '.jpg'; $mime = image_type_to_mime_type(IMAGETYPE_JPEG); imagejpeg($img); } imagedestroy($img); unset($img); $output = new QView_Output($filename, $mime, ob_get_clean()); $output->contentDisposition('inline')->enableClientCache(false); return $output; }
/** * */ protected function loadFontFile() { $result = false; $fpath = false; $pA = explode(PATH_SEPARATOR, get_include_path()); foreach ($pA as $path) { $fpath = $path . '/' . $this->font; if (file_exists($fpath)) { $result = true; break; } } if ($result === false) { throw new BuildException(self::thisName . ': invalid font file.'); } $this->font = imageloadfont($fpath); }
imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), $noise_color); } } if ($ttf_supported) { $textbox = imagettfbbox($font_size, 0, $font, $code); $x = ($width - $textbox[4]) / 2; $y = ($height - $textbox[5]) / 2; imagettftext($image, $font_size, 0, $x, $y, $text_color, $font, $code); } else { $x = 4; $y = 5; imagestring($image, $font_ballback, $x, $y, $code, $text_color); } /* create textbox and add text*/ $textbox = imagettfbbox($font_size, 0, $font, $code) or die('Error in imagettfbbox function'); $x = ($width - $textbox[4]) / 2; $y = ($height - $textbox[5]) / 2; imagettftext($image, $font_size, 0, $x, $y, $text_color, $font, $code) or die('Error in imagettftext function'); /*output captcha image to browser */ header('Content-Type: image/jpeg'); imagejpeg($image); imagedestroy($image); $_SESSION['security_code'] = $code; } } $width = 90; $height = 30; $characters = 6; $font = KU_ROOTDIR . 'lib/fonts/monofont.ttf'; $font_fallback = imageloadfont(KU_ROOTDIR . 'lib/fonts/captchafont.gdf'); $captcha = new CaptchaSecurityImages($width, $height, $characters, $font);
/** * captchaAction * * Create captcha */ public function captchaAction() { // Set views layout to empty $this->_helper->layout()->setLayout('empty'); // generate code with 5 characters $security_code = $this->generateCode(5); // start session so user input can be compared to $security_code $session = new Zend_Session_Namespace('registration'); $session->security_code = md5($security_code); // Set the image width and height $width = 150; $height = 48; // Create the image resource $image = ImageCreate($width, $height); // Set the font $font = imageloadfont("images/anonymous.gdf"); // define colors used in image $white = ImageColorAllocate($image, 255, 255, 255); $blue = ImageColorAllocate($image, 51, 102, 153); $green = ImageColorAllocate($image, 112, 191, 12); $black = ImageColorAllocate($image, 0, 0, 0); $grey = ImageColorAllocate($image, 150, 150, 150); //Make the background blue ImageFill($image, 0, 0, $blue); //Generate random(ish) position for image text $text_x = mt_rand(-2, 30); $text_y = mt_rand(-2, 10); //Add randomly generated string in white to the image ImageString($image, $font, $text_x, $text_y, strtoupper($security_code), $white); //Throw in some lines to make it a little bit harder for any bots to break $s = $width * $height / 500; for ($i = 0; $i < $s; $i++) { imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), $grey); } // end for // borders for captcha; syntax is (image, Xstart, Ystart, Xend, Yend, color) /* imageline($image, 0, 0, $width, 0, $blue); //topl-topr imageline($image, 0, $height-1, $width, $height-1, $blue); //btml-btmr imageline($image, 0, 0, 0, $height, $blue); //topl-btml imageline($image, $width-1, 0, $width-1, $height, $blue); //topr-btmr */ $this->view->image = $image; }
/** * Actually uploads the file, and act on it according to the set processing class variables * * This function copies the uploaded file to the given location, eventually performing actions on it. * Typically, you can call {@link process} several times for the same file, * for instance to create a resized image and a thumbnail of the same file. * The original uploaded file remains intact in its temporary location, so you can use {@link process} several times. * You will be able to delete the uploaded file with {@link clean} when you have finished all your {@link process} calls. * * According to the processing class variables set in the calling file, the file can be renamed, * and if it is an image, can be resized or converted. * * When the processing is completed, and the file copied to its new location, the * processing class variables will be reset to their default value. * This allows you to set new properties, and perform another {@link process} on the same uploaded file * * If the function is called with a null or empty argument, then it will return the content of the picture * * It will set {@link processed} (and {@link error} is an error occurred) * * @access public * @param string $server_path Optional path location of the uploaded file, with an ending slash * @return string Optional content of the image */ function process($server_path = null) { $this->error = ''; $this->processed = true; $return_mode = false; $return_content = null; // clean up dst variables $this->file_dst_path = ''; $this->file_dst_pathname = ''; $this->file_dst_name = ''; $this->file_dst_name_body = ''; $this->file_dst_name_ext = ''; if (!$this->uploaded) { $this->error = $this->translate('file_not_uploaded'); $this->processed = false; } if ($this->processed) { if (empty($server_path) || is_null($server_path)) { $this->log .= '<b>process file and return the content</b><br />'; $return_mode = true; } else { if (strtolower(substr(PHP_OS, 0, 3)) === 'win') { if (substr($server_path, -1, 1) != '\\') { $server_path = $server_path . '\\'; } } else { if (substr($server_path, -1, 1) != '/') { $server_path = $server_path . '/'; } } $this->log .= '<b>process file to ' . $server_path . '</b><br />'; } } if ($this->processed) { // checks file max size if ($this->file_src_size > $this->file_max_size) { $this->processed = false; $this->error = $this->translate('file_too_big'); } else { $this->log .= '- file size OK<br />'; } } if ($this->processed) { // if we have an image without extension, set it if ($this->file_is_image && !$this->file_src_name_ext_) { $this->file_src_name_ext = $this->file_src_name_ext_ = $this->image_src_type; } // turn dangerous scripts into text files if ($this->no_script) { if ((substr($this->file_src_mime, 0, 5) == 'text/' && $this->file_src_mime != 'text/rtf' || strpos($this->file_src_mime, 'javascript') !== false) && substr($this->file_src_name, -4) != '.txt' || preg_match('/\\.(php|pl|py|cgi|asp|js)$/i', $this->file_src_name) || empty($this->file_src_name_ext_)) { $this->file_src_mime = 'text/plain'; $this->log .= '- script ' . $this->file_src_name . ' renamed as ' . $this->file_src_name . '.txt!<br />'; $this->file_src_name_ext = $this->file_src_name_ext_ . (empty($this->file_src_name_ext_) ? 'txt' : '.txt'); } } if ($this->mime_check && empty($this->file_src_mime)) { $this->processed = false; $this->error = $this->translate('no_mime'); } else { if ($this->mime_check && !empty($this->file_src_mime) && strpos($this->file_src_mime, '/') !== false) { list($m1, $m2) = explode('/', $this->file_src_mime); $allowed = false; // check wether the mime type is allowed foreach ($this->allowed as $k => $v) { list($v1, $v2) = explode('/', $v); if ($v1 == '*' && $v2 == '*' || $v1 == $m1 && ($v2 == $m2 || $v2 == '*')) { $allowed = true; break; } } // check wether the mime type is forbidden foreach ($this->forbidden as $k => $v) { list($v1, $v2) = explode('/', $v); if ($v1 == '*' && $v2 == '*' || $v1 == $m1 && ($v2 == $m2 || $v2 == '*')) { $allowed = false; break; } } if (!$allowed) { $this->processed = false; $this->error = $this->translate('incorrect_file'); } else { $this->log .= '- file mime OK : ' . $this->file_src_mime . '<br />'; } } else { $this->log .= '- file mime (not checked) : ' . $this->file_src_mime . '<br />'; } } // if the file is an image, we can check on its dimensions // these checks are not available if open_basedir restrictions are in place if ($this->file_is_image) { if (is_numeric($this->image_src_x) && is_numeric($this->image_src_y)) { $ratio = $this->image_src_x / $this->image_src_y; if (!is_null($this->image_max_width) && $this->image_src_x > $this->image_max_width) { $this->processed = false; $this->error = $this->translate('image_too_wide'); } if (!is_null($this->image_min_width) && $this->image_src_x < $this->image_min_width) { $this->processed = false; $this->error = $this->translate('image_too_narrow'); } if (!is_null($this->image_max_height) && $this->image_src_y > $this->image_max_height) { $this->processed = false; $this->error = $this->translate('image_too_high'); } if (!is_null($this->image_min_height) && $this->image_src_y < $this->image_min_height) { $this->processed = false; $this->error = $this->translate('image_too_short'); } if (!is_null($this->image_max_ratio) && $ratio > $this->image_max_ratio) { $this->processed = false; $this->error = $this->translate('ratio_too_high'); } if (!is_null($this->image_min_ratio) && $ratio < $this->image_min_ratio) { $this->processed = false; $this->error = $this->translate('ratio_too_low'); } if (!is_null($this->image_max_pixels) && $this->image_src_pixels > $this->image_max_pixels) { $this->processed = false; $this->error = $this->translate('too_many_pixels'); } if (!is_null($this->image_min_pixels) && $this->image_src_pixels < $this->image_min_pixels) { $this->processed = false; $this->error = $this->translate('not_enough_pixels'); } } else { $this->log .= '- no image properties available, can\'t enforce dimension checks : ' . $this->file_src_mime . '<br />'; } } } if ($this->processed) { $this->file_dst_path = $server_path; // repopulate dst variables from src $this->file_dst_name = $this->file_src_name; $this->file_dst_name_body = $this->file_src_name_body; $this->file_dst_name_ext = $this->file_src_name_ext; if ($this->file_overwrite) { $this->file_auto_rename = false; } if ($this->image_convert != '') { // if we convert as an image $this->file_dst_name_ext = $this->image_convert; $this->log .= '- new file name ext : ' . $this->image_convert . '<br />'; } if ($this->file_new_name_body != '') { // rename file body $this->file_dst_name_body = $this->file_new_name_body; $this->log .= '- new file name body : ' . $this->file_new_name_body . '<br />'; } if ($this->file_new_name_ext != '') { // rename file ext $this->file_dst_name_ext = $this->file_new_name_ext; $this->log .= '- new file name ext : ' . $this->file_new_name_ext . '<br />'; } if ($this->file_name_body_add != '') { // append a string to the name $this->file_dst_name_body = $this->file_dst_name_body . $this->file_name_body_add; $this->log .= '- file name body append : ' . $this->file_name_body_add . '<br />'; } if ($this->file_name_body_pre != '') { // prepend a string to the name $this->file_dst_name_body = $this->file_name_body_pre . $this->file_dst_name_body; $this->log .= '- file name body prepend : ' . $this->file_name_body_pre . '<br />'; } if ($this->file_safe_name) { // formats the name $this->file_dst_name_body = str_replace(array(' ', '-'), array('_', '_'), $this->file_dst_name_body); $this->file_dst_name_body = preg_replace('/[^A-Za-z0-9_]/', '', $this->file_dst_name_body); $this->log .= '- file name safe format<br />'; } $this->log .= '- destination variables<br />'; if (empty($this->file_dst_path) || is_null($this->file_dst_path)) { $this->log .= ' file_dst_path : n/a<br />'; } else { $this->log .= ' file_dst_path : ' . $this->file_dst_path . '<br />'; } $this->log .= ' file_dst_name_body : ' . $this->file_dst_name_body . '<br />'; $this->log .= ' file_dst_name_ext : ' . $this->file_dst_name_ext . '<br />'; // do we do some image manipulation? $image_manipulation = $this->file_is_image && ($this->image_resize || $this->image_convert != '' || is_numeric($this->image_brightness) || is_numeric($this->image_contrast) || is_numeric($this->image_threshold) || !empty($this->image_tint_color) || !empty($this->image_overlay_color) || $this->image_unsharp || !empty($this->image_text) || $this->image_greyscale || $this->image_negative || !empty($this->image_watermark) || is_numeric($this->image_rotate) || is_numeric($this->jpeg_size) || !empty($this->image_flip) || !empty($this->image_crop) || !empty($this->image_precrop) || !empty($this->image_border) || $this->image_frame > 0 || $this->image_bevel > 0 || $this->image_reflection_height); if ($image_manipulation) { if ($this->image_convert == '') { $this->file_dst_name = $this->file_dst_name_body . (!empty($this->file_dst_name_ext) ? '.' . $this->file_dst_name_ext : ''); $this->log .= '- image operation, keep extension<br />'; } else { $this->file_dst_name = $this->file_dst_name_body . '.' . $this->image_convert; $this->log .= '- image operation, change extension for conversion type<br />'; } } else { $this->file_dst_name = $this->file_dst_name_body . (!empty($this->file_dst_name_ext) ? '.' . $this->file_dst_name_ext : ''); $this->log .= '- no image operation, keep extension<br />'; } if (!$return_mode) { if (!$this->file_auto_rename) { $this->log .= '- no auto_rename if same filename exists<br />'; $this->file_dst_pathname = $this->file_dst_path . $this->file_dst_name; } else { $this->log .= '- checking for auto_rename<br />'; $this->file_dst_pathname = $this->file_dst_path . $this->file_dst_name; $body = $this->file_dst_name_body; $cpt = 1; while (@file_exists($this->file_dst_pathname)) { $this->file_dst_name_body = $body . '_' . $cpt; $this->file_dst_name = $this->file_dst_name_body . (!empty($this->file_dst_name_ext) ? '.' . $this->file_dst_name_ext : ''); $cpt++; $this->file_dst_pathname = $this->file_dst_path . $this->file_dst_name; } if ($cpt > 1) { $this->log .= ' auto_rename to ' . $this->file_dst_name . '<br />'; } } $this->log .= '- destination file details<br />'; $this->log .= ' file_dst_name : ' . $this->file_dst_name . '<br />'; $this->log .= ' file_dst_pathname : ' . $this->file_dst_pathname . '<br />'; if ($this->file_overwrite) { $this->log .= '- no overwrite checking<br />'; } else { if (@file_exists($this->file_dst_pathname)) { $this->processed = false; $this->error = $this->translate('already_exists', array($this->file_dst_name)); } else { $this->log .= '- ' . $this->file_dst_name . ' doesn\'t exist already<br />'; } } } } if ($this->processed) { // if we have already moved the uploaded file, we use the temporary copy as source file, and check if it exists if (!empty($this->file_src_temp)) { $this->log .= '- use the temp file instead of the original file since it is a second process<br />'; $this->file_src_pathname = $this->file_src_temp; if (!file_exists($this->file_src_pathname)) { $this->processed = false; $this->error = $this->translate('temp_file_missing'); } // if we haven't a temp file, and that we do check on uploads, we use is_uploaded_file() } else { if (!$this->no_upload_check) { if (!is_uploaded_file($this->file_src_pathname)) { $this->processed = false; $this->error = $this->translate('source_missing'); } // otherwise, if we don't check on uploaded files (local file for instance), we use file_exists() } else { if (!file_exists($this->file_src_pathname)) { $this->processed = false; $this->error = $this->translate('source_missing'); } } } // checks if the destination directory exists, and attempt to create it if (!$return_mode) { if ($this->processed && !file_exists($this->file_dst_path)) { if ($this->dir_auto_create) { $this->log .= '- ' . $this->file_dst_path . ' doesn\'t exist. Attempting creation:'; if (!$this->rmkdir($this->file_dst_path, $this->dir_chmod)) { $this->log .= ' failed<br />'; $this->processed = false; $this->error = $this->translate('destination_dir'); } else { $this->log .= ' success<br />'; } } else { $this->error = $this->translate('destination_dir_missing'); } } if ($this->processed && !is_dir($this->file_dst_path)) { $this->processed = false; $this->error = $this->translate('destination_path_not_dir'); } // checks if the destination directory is writeable, and attempt to make it writeable $hash = md5($this->file_dst_name_body . rand(1, 1000)); if ($this->processed && !($f = @fopen($this->file_dst_path . $hash . '.' . $this->file_dst_name_ext, 'a+'))) { if ($this->dir_auto_chmod) { $this->log .= '- ' . $this->file_dst_path . ' is not writeable. Attempting chmod:'; if (!@chmod($this->file_dst_path, $this->dir_chmod)) { $this->log .= ' failed<br />'; $this->processed = false; $this->error = $this->translate('destination_dir_write'); } else { $this->log .= ' success<br />'; if (!($f = @fopen($this->file_dst_path . $hash . '.' . $this->file_dst_name_ext, 'a+'))) { // we re-check $this->processed = false; $this->error = $this->translate('destination_dir_write'); } else { @fclose($f); } } } else { $this->processed = false; $this->error = $this->translate('destination_path_write'); } } else { if ($this->processed) { @fclose($f); } @unlink($this->file_dst_path . $hash . '.' . $this->file_dst_name_ext); } // if we have an uploaded file, and if it is the first process, and if we can't access the file directly (open_basedir restriction) // then we create a temp file that will be used as the source file in subsequent processes // the third condition is there to check if the file is not accessible *directly* (it already has positively gone through is_uploaded_file(), so it exists) if (!$this->no_upload_check && empty($this->file_src_temp) && !@file_exists($this->file_src_pathname)) { $this->log .= '- attempting to use a temp file:'; $hash = md5($this->file_dst_name_body . rand(1, 1000)); if (move_uploaded_file($this->file_src_pathname, $this->file_dst_path . $hash . '.' . $this->file_dst_name_ext)) { $this->file_src_pathname = $this->file_dst_path . $hash . '.' . $this->file_dst_name_ext; $this->file_src_temp = $this->file_src_pathname; $this->log .= ' file created<br />'; $this->log .= ' temp file is: ' . $this->file_src_temp . '<br />'; } else { $this->log .= ' failed<br />'; $this->processed = false; $this->error = $this->translate('temp_file'); } } } } if ($this->processed) { // we do a quick check to ensure the file is really an image // we can do this only now, as it would have failed before in case of open_basedir if ($image_manipulation && !@getimagesize($this->file_src_pathname)) { $this->log .= '- the file is not an image!<br />'; $image_manipulation = false; } if ($image_manipulation) { // checks if the source file is readable if ($this->processed && !($f = @fopen($this->file_src_pathname, 'r'))) { $this->processed = false; $this->error = $this->translate('source_not_readable'); } else { @fclose($f); } // we now do all the image manipulations $this->log .= '- image resizing or conversion wanted<br />'; if ($this->gdversion()) { switch ($this->image_src_type) { case 'jpg': if (!function_exists('imagecreatefromjpeg')) { $this->processed = false; $this->error = $this->translate('no_create_support', array('JPEG')); } else { $image_src = @imagecreatefromjpeg($this->file_src_pathname); if (!$image_src) { $this->processed = false; $this->error = $this->translate('create_error', array('JPEG')); } else { $this->log .= '- source image is JPEG<br />'; } } break; case 'png': if (!function_exists('imagecreatefrompng')) { $this->processed = false; $this->error = $this->translate('no_create_support', array('PNG')); } else { $image_src = @imagecreatefrompng($this->file_src_pathname); if (!$image_src) { $this->processed = false; $this->error = $this->translate('create_error', array('PNG')); } else { $this->log .= '- source image is PNG<br />'; } } break; case 'gif': if (!function_exists('imagecreatefromgif')) { $this->processed = false; $this->error = $this->translate('no_create_support', array('GIF')); } else { $image_src = @imagecreatefromgif($this->file_src_pathname); if (!$image_src) { $this->processed = false; $this->error = $this->translate('create_error', array('GIF')); } else { $this->log .= '- source image is GIF<br />'; } } break; case 'bmp': if (!method_exists($this, 'imagecreatefrombmp')) { $this->processed = false; $this->error = $this->translate('no_create_support', array('BMP')); } else { $image_src = @$this->imagecreatefrombmp($this->file_src_pathname); if (!$image_src) { $this->processed = false; $this->error = $this->translate('create_error', array('BMP')); } else { $this->log .= '- source image is BMP<br />'; } } break; default: $this->processed = false; $this->error = $this->translate('source_invalid'); } } else { $this->processed = false; $this->error = $this->translate('gd_missing'); } if ($this->processed && $image_src) { // we have to set image_convert if it is not already if (empty($this->image_convert)) { $this->log .= '- setting destination file type to ' . $this->file_src_name_ext . '<br />'; $this->image_convert = $this->file_src_name_ext; } if (!in_array($this->image_convert, $this->image_supported)) { $this->image_convert = 'jpg'; } // we set the default color to be the background color if we don't output in a transparent format if ($this->image_convert != 'png' && $this->image_convert != 'gif' && !empty($this->image_default_color) && empty($this->image_background_color)) { $this->image_background_color = $this->image_default_color; } if (!empty($this->image_background_color)) { $this->image_default_color = $this->image_background_color; } if (empty($this->image_default_color)) { $this->image_default_color = '#FFFFFF'; } $this->image_src_x = imagesx($image_src); $this->image_src_y = imagesy($image_src); $gd_version = $this->gdversion(); $ratio_crop = null; if (!imageistruecolor($image_src)) { // $this->image_src_type == 'gif' $this->log .= '- image is detected as having a palette<br />'; $this->image_is_palette = true; $this->image_transparent_color = imagecolortransparent($image_src); if ($this->image_transparent_color >= 0 && imagecolorstotal($image_src) > $this->image_transparent_color) { $this->image_is_transparent = true; $this->log .= ' palette image is detected as transparent<br />'; } // if the image has a palette (GIF), we convert it to true color, preserving transparency $this->log .= ' convert palette image to true color<br />'; $true_color = imagecreatetruecolor($this->image_src_x, $this->image_src_y); imagealphablending($true_color, false); imagesavealpha($true_color, true); for ($x = 0; $x < $this->image_src_x; $x++) { for ($y = 0; $y < $this->image_src_y; $y++) { if ($this->image_transparent_color >= 0 && imagecolorat($image_src, $x, $y) == $this->image_transparent_color) { imagesetpixel($true_color, $x, $y, 127 << 24); } else { $rgb = imagecolorsforindex($image_src, imagecolorat($image_src, $x, $y)); imagesetpixel($true_color, $x, $y, $rgb['alpha'] << 24 | $rgb['red'] << 16 | $rgb['green'] << 8 | $rgb['blue']); } } } $image_src = $this->imagetransfer($true_color, $image_src); imagealphablending($image_src, false); imagesavealpha($image_src, true); $this->image_is_palette = false; } $image_dst =& $image_src; // pre-crop image, before resizing if (!empty($this->image_precrop)) { if (is_array($this->image_precrop)) { $vars = $this->image_precrop; } else { $vars = explode(' ', $this->image_precrop); } if (sizeof($vars) == 4) { $ct = $vars[0]; $cr = $vars[1]; $cb = $vars[2]; $cl = $vars[3]; } else { if (sizeof($vars) == 2) { $ct = $vars[0]; $cr = $vars[1]; $cb = $vars[0]; $cl = $vars[1]; } else { $ct = $vars[0]; $cr = $vars[0]; $cb = $vars[0]; $cl = $vars[0]; } } if (strpos($ct, '%') > 0) { $ct = $this->image_src_y * (str_replace('%', '', $ct) / 100); } if (strpos($cr, '%') > 0) { $cr = $this->image_src_x * (str_replace('%', '', $cr) / 100); } if (strpos($cb, '%') > 0) { $cb = $this->image_src_y * (str_replace('%', '', $cb) / 100); } if (strpos($cl, '%') > 0) { $cl = $this->image_src_x * (str_replace('%', '', $cl) / 100); } if (strpos($ct, 'px') > 0) { $ct = str_replace('px', '', $ct); } if (strpos($cr, 'px') > 0) { $cr = str_replace('px', '', $cr); } if (strpos($cb, 'px') > 0) { $cb = str_replace('px', '', $cb); } if (strpos($cl, 'px') > 0) { $cl = str_replace('px', '', $cl); } $ct = (int) $ct; $cr = (int) $cr; $cb = (int) $cb; $cl = (int) $cl; $this->log .= '- pre-crop image : ' . $ct . ' ' . $cr . ' ' . $cb . ' ' . $cl . ' <br />'; $this->image_src_x = $this->image_src_x - $cl - $cr; $this->image_src_y = $this->image_src_y - $ct - $cb; if ($this->image_src_x < 1) { $this->image_src_x = 1; } if ($this->image_src_y < 1) { $this->image_src_y = 1; } $tmp = $this->imagecreatenew($this->image_src_x, $this->image_src_y); // we copy the image into the recieving image imagecopy($tmp, $image_dst, 0, 0, $cl, $ct, $this->image_src_x, $this->image_src_y); // if we crop with negative margins, we have to make sure the extra bits are the right color, or transparent if ($ct < 0 || $cr < 0 || $cb < 0 || $cl < 0) { // use the background color if present if (!empty($this->image_background_color)) { list($red, $green, $blue) = $this->getcolors($this->image_background_color); $fill = imagecolorallocate($tmp, $red, $green, $blue); } else { $fill = imagecolorallocatealpha($tmp, 0, 0, 0, 127); } // fills eventual negative margins if ($ct < 0) { imagefilledrectangle($tmp, 0, 0, $this->image_src_x, -$ct, $fill); } if ($cr < 0) { imagefilledrectangle($tmp, $this->image_src_x + $cr, 0, $this->image_src_x, $this->image_src_y, $fill); } if ($cb < 0) { imagefilledrectangle($tmp, 0, $this->image_src_y + $cb, $this->image_src_x, $this->image_src_y, $fill); } if ($cl < 0) { imagefilledrectangle($tmp, 0, 0, -$cl, $this->image_src_y, $fill); } } // we transfert tmp into image_dst $image_dst = $this->imagetransfer($tmp, $image_dst); } // resize image (and move image_src_x, image_src_y dimensions into image_dst_x, image_dst_y) if ($this->image_resize) { $this->log .= '- resizing...<br />'; if ($this->image_ratio_x) { $this->log .= ' calculate x size<br />'; $this->image_dst_x = round($this->image_src_x * $this->image_y / $this->image_src_y); $this->image_dst_y = $this->image_y; } else { if ($this->image_ratio_y) { $this->log .= ' calculate y size<br />'; $this->image_dst_x = $this->image_x; $this->image_dst_y = round($this->image_src_y * $this->image_x / $this->image_src_x); } else { if (is_numeric($this->image_ratio_pixels)) { $this->log .= ' calculate x/y size to match a number of pixels<br />'; $pixels = $this->image_src_y * $this->image_src_x; $diff = sqrt($this->image_ratio_pixels / $pixels); $this->image_dst_x = round($this->image_src_x * $diff); $this->image_dst_y = round($this->image_src_y * $diff); } else { if ($this->image_ratio || $this->image_ratio_crop || $this->image_ratio_fill || $this->image_ratio_no_zoom_in || $this->image_ratio_no_zoom_out) { $this->log .= ' check x/y sizes<br />'; if (!$this->image_ratio_no_zoom_in && !$this->image_ratio_no_zoom_out || $this->image_ratio_no_zoom_in && ($this->image_src_x > $this->image_x || $this->image_src_y > $this->image_y) || $this->image_ratio_no_zoom_out && $this->image_src_x < $this->image_x && $this->image_src_y < $this->image_y) { $this->image_dst_x = $this->image_x; $this->image_dst_y = $this->image_y; if ($this->image_ratio_crop) { if (!is_string($this->image_ratio_crop)) { $this->image_ratio_crop = ''; } $this->image_ratio_crop = strtolower($this->image_ratio_crop); if ($this->image_src_x / $this->image_x > $this->image_src_y / $this->image_y) { $this->image_dst_y = $this->image_y; $this->image_dst_x = intval($this->image_src_x * ($this->image_y / $this->image_src_y)); $ratio_crop = array(); $ratio_crop['x'] = $this->image_dst_x - $this->image_x; if (strpos($this->image_ratio_crop, 'l') !== false) { $ratio_crop['l'] = 0; $ratio_crop['r'] = $ratio_crop['x']; } else { if (strpos($this->image_ratio_crop, 'r') !== false) { $ratio_crop['l'] = $ratio_crop['x']; $ratio_crop['r'] = 0; } else { $ratio_crop['l'] = round($ratio_crop['x'] / 2); $ratio_crop['r'] = $ratio_crop['x'] - $ratio_crop['l']; } } $this->log .= ' ratio_crop_x : ' . $ratio_crop['x'] . ' (' . $ratio_crop['l'] . ';' . $ratio_crop['r'] . ')<br />'; if (is_null($this->image_crop)) { $this->image_crop = array(0, 0, 0, 0); } } else { $this->image_dst_x = $this->image_x; $this->image_dst_y = intval($this->image_src_y * ($this->image_x / $this->image_src_x)); $ratio_crop = array(); $ratio_crop['y'] = $this->image_dst_y - $this->image_y; if (strpos($this->image_ratio_crop, 't') !== false) { $ratio_crop['t'] = 0; $ratio_crop['b'] = $ratio_crop['y']; } else { if (strpos($this->image_ratio_crop, 'b') !== false) { $ratio_crop['t'] = $ratio_crop['y']; $ratio_crop['b'] = 0; } else { $ratio_crop['t'] = round($ratio_crop['y'] / 2); $ratio_crop['b'] = $ratio_crop['y'] - $ratio_crop['t']; } } $this->log .= ' ratio_crop_y : ' . $ratio_crop['y'] . ' (' . $ratio_crop['t'] . ';' . $ratio_crop['b'] . ')<br />'; if (is_null($this->image_crop)) { $this->image_crop = array(0, 0, 0, 0); } } } else { if ($this->image_ratio_fill) { if (!is_string($this->image_ratio_fill)) { $this->image_ratio_fill = ''; } $this->image_ratio_fill = strtolower($this->image_ratio_fill); if ($this->image_src_x / $this->image_x < $this->image_src_y / $this->image_y) { $this->image_dst_y = $this->image_y; $this->image_dst_x = intval($this->image_src_x * ($this->image_y / $this->image_src_y)); $ratio_crop = array(); $ratio_crop['x'] = $this->image_dst_x - $this->image_x; if (strpos($this->image_ratio_fill, 'l') !== false) { $ratio_crop['l'] = 0; $ratio_crop['r'] = $ratio_crop['x']; } else { if (strpos($this->image_ratio_fill, 'r') !== false) { $ratio_crop['l'] = $ratio_crop['x']; $ratio_crop['r'] = 0; } else { $ratio_crop['l'] = round($ratio_crop['x'] / 2); $ratio_crop['r'] = $ratio_crop['x'] - $ratio_crop['l']; } } $this->log .= ' ratio_fill_x : ' . $ratio_crop['x'] . ' (' . $ratio_crop['l'] . ';' . $ratio_crop['r'] . ')<br />'; if (is_null($this->image_crop)) { $this->image_crop = array(0, 0, 0, 0); } } else { $this->image_dst_x = $this->image_x; $this->image_dst_y = intval($this->image_src_y * ($this->image_x / $this->image_src_x)); $ratio_crop = array(); $ratio_crop['y'] = $this->image_dst_y - $this->image_y; if (strpos($this->image_ratio_fill, 't') !== false) { $ratio_crop['t'] = 0; $ratio_crop['b'] = $ratio_crop['y']; } else { if (strpos($this->image_ratio_fill, 'b') !== false) { $ratio_crop['t'] = $ratio_crop['y']; $ratio_crop['b'] = 0; } else { $ratio_crop['t'] = round($ratio_crop['y'] / 2); $ratio_crop['b'] = $ratio_crop['y'] - $ratio_crop['t']; } } $this->log .= ' ratio_fill_y : ' . $ratio_crop['y'] . ' (' . $ratio_crop['t'] . ';' . $ratio_crop['b'] . ')<br />'; if (is_null($this->image_crop)) { $this->image_crop = array(0, 0, 0, 0); } } } else { if ($this->image_src_x / $this->image_x > $this->image_src_y / $this->image_y) { $this->image_dst_x = $this->image_x; $this->image_dst_y = intval($this->image_src_y * ($this->image_x / $this->image_src_x)); } else { $this->image_dst_y = $this->image_y; $this->image_dst_x = intval($this->image_src_x * ($this->image_y / $this->image_src_y)); } } } } else { $this->log .= ' doesn\'t calculate x/y sizes<br />'; $this->image_dst_x = $this->image_src_x; $this->image_dst_y = $this->image_src_y; } } else { $this->log .= ' use plain sizes<br />'; $this->image_dst_x = $this->image_x; $this->image_dst_y = $this->image_y; } } } } if ($this->image_dst_x < 1) { $this->image_dst_x = 1; } if ($this->image_dst_y < 1) { $this->image_dst_y = 1; } $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y); if ($gd_version >= 2) { $res = imagecopyresampled($tmp, $image_src, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y, $this->image_src_x, $this->image_src_y); } else { $res = imagecopyresized($tmp, $image_src, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y, $this->image_src_x, $this->image_src_y); } $this->log .= ' resized image object created<br />'; $this->log .= ' image_src_x y : ' . $this->image_src_x . ' x ' . $this->image_src_y . '<br />'; $this->log .= ' image_dst_x y : ' . $this->image_dst_x . ' x ' . $this->image_dst_y . '<br />'; // we transfert tmp into image_dst $image_dst = $this->imagetransfer($tmp, $image_dst); } else { $this->image_dst_x = $this->image_src_x; $this->image_dst_y = $this->image_src_y; } // crop image (and also crops if image_ratio_crop is used) if (!empty($this->image_crop) || !is_null($ratio_crop)) { if (is_array($this->image_crop)) { $vars = $this->image_crop; } else { $vars = explode(' ', $this->image_crop); } if (sizeof($vars) == 4) { $ct = $vars[0]; $cr = $vars[1]; $cb = $vars[2]; $cl = $vars[3]; } else { if (sizeof($vars) == 2) { $ct = $vars[0]; $cr = $vars[1]; $cb = $vars[0]; $cl = $vars[1]; } else { $ct = $vars[0]; $cr = $vars[0]; $cb = $vars[0]; $cl = $vars[0]; } } if (strpos($ct, '%') > 0) { $ct = $this->image_dst_y * (str_replace('%', '', $ct) / 100); } if (strpos($cr, '%') > 0) { $cr = $this->image_dst_x * (str_replace('%', '', $cr) / 100); } if (strpos($cb, '%') > 0) { $cb = $this->image_dst_y * (str_replace('%', '', $cb) / 100); } if (strpos($cl, '%') > 0) { $cl = $this->image_dst_x * (str_replace('%', '', $cl) / 100); } if (strpos($ct, 'px') > 0) { $ct = str_replace('px', '', $ct); } if (strpos($cr, 'px') > 0) { $cr = str_replace('px', '', $cr); } if (strpos($cb, 'px') > 0) { $cb = str_replace('px', '', $cb); } if (strpos($cl, 'px') > 0) { $cl = str_replace('px', '', $cl); } $ct = (int) $ct; $cr = (int) $cr; $cb = (int) $cb; $cl = (int) $cl; // we adjust the cropping if we use image_ratio_crop if (!is_null($ratio_crop)) { if (array_key_exists('t', $ratio_crop)) { $ct += $ratio_crop['t']; } if (array_key_exists('r', $ratio_crop)) { $cr += $ratio_crop['r']; } if (array_key_exists('b', $ratio_crop)) { $cb += $ratio_crop['b']; } if (array_key_exists('l', $ratio_crop)) { $cl += $ratio_crop['l']; } } $this->log .= '- crop image : ' . $ct . ' ' . $cr . ' ' . $cb . ' ' . $cl . ' <br />'; $this->image_dst_x = $this->image_dst_x - $cl - $cr; $this->image_dst_y = $this->image_dst_y - $ct - $cb; if ($this->image_dst_x < 1) { $this->image_dst_x = 1; } if ($this->image_dst_y < 1) { $this->image_dst_y = 1; } $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y); // we copy the image into the recieving image imagecopy($tmp, $image_dst, 0, 0, $cl, $ct, $this->image_dst_x, $this->image_dst_y); // if we crop with negative margins, we have to make sure the extra bits are the right color, or transparent if ($ct < 0 || $cr < 0 || $cb < 0 || $cl < 0) { // use the background color if present if (!empty($this->image_background_color)) { list($red, $green, $blue) = $this->getcolors($this->image_background_color); $fill = imagecolorallocate($tmp, $red, $green, $blue); } else { $fill = imagecolorallocatealpha($tmp, 0, 0, 0, 127); } // fills eventual negative margins if ($ct < 0) { imagefilledrectangle($tmp, 0, 0, $this->image_dst_x, -$ct - 1, $fill); } if ($cr < 0) { imagefilledrectangle($tmp, $this->image_dst_x + $cr, 0, $this->image_dst_x, $this->image_dst_y, $fill); } if ($cb < 0) { imagefilledrectangle($tmp, 0, $this->image_dst_y + $cb, $this->image_dst_x, $this->image_dst_y, $fill); } if ($cl < 0) { imagefilledrectangle($tmp, 0, 0, -$cl - 1, $this->image_dst_y, $fill); } } // we transfert tmp into image_dst $image_dst = $this->imagetransfer($tmp, $image_dst); } // flip image if ($gd_version >= 2 && !empty($this->image_flip)) { $this->image_flip = strtolower($this->image_flip); $this->log .= '- flip image : ' . $this->image_flip . '<br />'; $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y); for ($x = 0; $x < $this->image_dst_x; $x++) { for ($y = 0; $y < $this->image_dst_y; $y++) { if (strpos($this->image_flip, 'v') !== false) { imagecopy($tmp, $image_dst, $this->image_dst_x - $x - 1, $y, $x, $y, 1, 1); } else { imagecopy($tmp, $image_dst, $x, $this->image_dst_y - $y - 1, $x, $y, 1, 1); } } } // we transfert tmp into image_dst $image_dst = $this->imagetransfer($tmp, $image_dst); } // rotate image if ($gd_version >= 2 && is_numeric($this->image_rotate)) { if (!in_array($this->image_rotate, array(0, 90, 180, 270))) { $this->image_rotate = 0; } if ($this->image_rotate != 0) { if ($this->image_rotate == 90 || $this->image_rotate == 270) { $tmp = $this->imagecreatenew($this->image_dst_y, $this->image_dst_x); } else { $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y); } $this->log .= '- rotate image : ' . $this->image_rotate . '<br />'; for ($x = 0; $x < $this->image_dst_x; $x++) { for ($y = 0; $y < $this->image_dst_y; $y++) { if ($this->image_rotate == 90) { imagecopy($tmp, $image_dst, $y, $x, $x, $this->image_dst_y - $y - 1, 1, 1); } else { if ($this->image_rotate == 180) { imagecopy($tmp, $image_dst, $x, $y, $this->image_dst_x - $x - 1, $this->image_dst_y - $y - 1, 1, 1); } else { if ($this->image_rotate == 270) { imagecopy($tmp, $image_dst, $y, $x, $this->image_dst_x - $x - 1, $y, 1, 1); } else { imagecopy($tmp, $image_dst, $x, $y, $x, $y, 1, 1); } } } } } if ($this->image_rotate == 90 || $this->image_rotate == 270) { $t = $this->image_dst_y; $this->image_dst_y = $this->image_dst_x; $this->image_dst_x = $t; } // we transfert tmp into image_dst $image_dst = $this->imagetransfer($tmp, $image_dst); } } // unsharp mask if ($gd_version >= 2 && $this->image_unsharp && is_numeric($this->image_unsharp_amount) && is_numeric($this->image_unsharp_radius) && is_numeric($this->image_unsharp_threshold)) { // Unsharp Mask for PHP - version 2.1.1 // Unsharp mask algorithm by Torstein Hønsi 2003-07. // Used with permission // Modified to support alpha transparency if ($this->image_unsharp_amount > 500) { $this->image_unsharp_amount = 500; } $this->image_unsharp_amount = $this->image_unsharp_amount * 0.016; if ($this->image_unsharp_radius > 50) { $this->image_unsharp_radius = 50; } $this->image_unsharp_radius = $this->image_unsharp_radius * 2; if ($this->image_unsharp_threshold > 255) { $this->image_unsharp_threshold = 255; } $this->image_unsharp_radius = abs(round($this->image_unsharp_radius)); if ($this->image_unsharp_radius != 0) { $this->image_dst_x = imagesx($image_dst); $this->image_dst_y = imagesy($image_dst); $canvas = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y, false, true); $blur = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y, false, true); if (function_exists('imageconvolution')) { // PHP >= 5.1 $matrix = array(array(1, 2, 1), array(2, 4, 2), array(1, 2, 1)); imagecopy($blur, $image_dst, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y); imageconvolution($blur, $matrix, 16, 0); } else { for ($i = 0; $i < $this->image_unsharp_radius; $i++) { imagecopy($blur, $image_dst, 0, 0, 1, 0, $this->image_dst_x - 1, $this->image_dst_y); // left $this->imagecopymergealpha($blur, $image_dst, 1, 0, 0, 0, $this->image_dst_x, $this->image_dst_y, 50); // right $this->imagecopymergealpha($blur, $image_dst, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y, 50); // center imagecopy($canvas, $blur, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y); $this->imagecopymergealpha($blur, $canvas, 0, 0, 0, 1, $this->image_dst_x, $this->image_dst_y - 1, 33.33333); // up $this->imagecopymergealpha($blur, $canvas, 0, 1, 0, 0, $this->image_dst_x, $this->image_dst_y, 25); // down } } $p_new = array(); if ($this->image_unsharp_threshold > 0) { for ($x = 0; $x < $this->image_dst_x - 1; $x++) { for ($y = 0; $y < $this->image_dst_y; $y++) { $p_orig = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y)); $p_blur = imagecolorsforindex($blur, imagecolorat($blur, $x, $y)); $p_new['red'] = abs($p_orig['red'] - $p_blur['red']) >= $this->image_unsharp_threshold ? max(0, min(255, $this->image_unsharp_amount * ($p_orig['red'] - $p_blur['red']) + $p_orig['red'])) : $p_orig['red']; $p_new['green'] = abs($p_orig['green'] - $p_blur['green']) >= $this->image_unsharp_threshold ? max(0, min(255, $this->image_unsharp_amount * ($p_orig['green'] - $p_blur['green']) + $p_orig['green'])) : $p_orig['green']; $p_new['blue'] = abs($p_orig['blue'] - $p_blur['blue']) >= $this->image_unsharp_threshold ? max(0, min(255, $this->image_unsharp_amount * ($p_orig['blue'] - $p_blur['blue']) + $p_orig['blue'])) : $p_orig['blue']; if ($p_orig['red'] != $p_new['red'] || $p_orig['green'] != $p_new['green'] || $p_orig['blue'] != $p_new['blue']) { $color = imagecolorallocatealpha($image_dst, $p_new['red'], $p_new['green'], $p_new['blue'], $p_orig['alpha']); imagesetpixel($image_dst, $x, $y, $color); } } } } else { for ($x = 0; $x < $this->image_dst_x; $x++) { for ($y = 0; $y < $this->image_dst_y; $y++) { $p_orig = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y)); $p_blur = imagecolorsforindex($blur, imagecolorat($blur, $x, $y)); $p_new['red'] = $this->image_unsharp_amount * ($p_orig['red'] - $p_blur['red']) + $p_orig['red']; if ($p_new['red'] > 255) { $p_new['red'] = 255; } elseif ($p_new['red'] < 0) { $p_new['red'] = 0; } $p_new['green'] = $this->image_unsharp_amount * ($p_orig['green'] - $p_blur['green']) + $p_orig['green']; if ($p_new['green'] > 255) { $p_new['green'] = 255; } elseif ($p_new['green'] < 0) { $p_new['green'] = 0; } $p_new['blue'] = $this->image_unsharp_amount * ($p_orig['blue'] - $p_blur['blue']) + $p_orig['blue']; if ($p_new['blue'] > 255) { $p_new['blue'] = 255; } elseif ($p_new['blue'] < 0) { $p_new['blue'] = 0; } $color = imagecolorallocatealpha($image_dst, $p_new['red'], $p_new['green'], $p_new['blue'], $p_orig['alpha']); imagesetpixel($image_dst, $x, $y, $color); } } } imagedestroy($canvas); imagedestroy($blur); } } // add color overlay if ($gd_version >= 2 && (is_numeric($this->image_overlay_percent) && $this->image_overlay_percent > 0 && !empty($this->image_overlay_color))) { $this->log .= '- apply color overlay<br />'; list($red, $green, $blue) = $this->getcolors($this->image_overlay_color); $filter = imagecreatetruecolor($this->image_dst_x, $this->image_dst_y); $color = imagecolorallocate($filter, $red, $green, $blue); imagefilledrectangle($filter, 0, 0, $this->image_dst_x, $this->image_dst_y, $color); $this->imagecopymergealpha($image_dst, $filter, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y, $this->image_overlay_percent); imagedestroy($filter); } // add brightness, contrast and tint, turns to greyscale and inverts colors if ($gd_version >= 2 && ($this->image_negative || $this->image_greyscale || is_numeric($this->image_threshold) || is_numeric($this->image_brightness) || is_numeric($this->image_contrast) || !empty($this->image_tint_color))) { $this->log .= '- apply tint, light, contrast correction, negative, greyscale and threshold<br />'; if (!empty($this->image_tint_color)) { list($tint_red, $tint_green, $tint_blue) = $this->getcolors($this->image_tint_color); } imagealphablending($image_dst, true); for ($y = 0; $y < $this->image_dst_y; $y++) { for ($x = 0; $x < $this->image_dst_x; $x++) { if ($this->image_greyscale) { $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y)); $r = $g = $b = round(0.2125 * $pixel['red'] + 0.7154 * $pixel['green'] + 0.0721 * $pixel['blue']); $color = imagecolorallocatealpha($image_dst, $r, $g, $b, $pixel['alpha']); imagesetpixel($image_dst, $x, $y, $color); } if (is_numeric($this->image_threshold)) { $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y)); $c = round($pixel['red'] + $pixel['green'] + $pixel['blue']) / 3 - 127; $r = $g = $b = $c > $this->image_threshold ? 255 : 0; $color = imagecolorallocatealpha($image_dst, $r, $g, $b, $pixel['alpha']); imagesetpixel($image_dst, $x, $y, $color); } if (is_numeric($this->image_brightness)) { $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y)); $r = max(min(round($pixel['red'] + $this->image_brightness * 2), 255), 0); $g = max(min(round($pixel['green'] + $this->image_brightness * 2), 255), 0); $b = max(min(round($pixel['blue'] + $this->image_brightness * 2), 255), 0); $color = imagecolorallocatealpha($image_dst, $r, $g, $b, $pixel['alpha']); imagesetpixel($image_dst, $x, $y, $color); } if (is_numeric($this->image_contrast)) { $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y)); $r = max(min(round(($this->image_contrast + 128) * $pixel['red'] / 128), 255), 0); $g = max(min(round(($this->image_contrast + 128) * $pixel['green'] / 128), 255), 0); $b = max(min(round(($this->image_contrast + 128) * $pixel['blue'] / 128), 255), 0); $color = imagecolorallocatealpha($image_dst, $r, $g, $b, $pixel['alpha']); imagesetpixel($image_dst, $x, $y, $color); } if (!empty($this->image_tint_color)) { $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y)); $r = min(round($tint_red * $pixel['red'] / 169), 255); $g = min(round($tint_green * $pixel['green'] / 169), 255); $b = min(round($tint_blue * $pixel['blue'] / 169), 255); $color = imagecolorallocatealpha($image_dst, $r, $g, $b, $pixel['alpha']); imagesetpixel($image_dst, $x, $y, $color); } if (!empty($this->image_negative)) { $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y)); $r = round(255 - $pixel['red']); $g = round(255 - $pixel['green']); $b = round(255 - $pixel['blue']); $color = imagecolorallocatealpha($image_dst, $r, $g, $b, $pixel['alpha']); imagesetpixel($image_dst, $x, $y, $color); } } } } // adds a border if ($gd_version >= 2 && !empty($this->image_border)) { if (is_array($this->image_border)) { $vars = $this->image_border; $this->log .= '- add border : ' . implode(' ', $this->image_border) . '<br />'; } else { $this->log .= '- add border : ' . $this->image_border . '<br />'; $vars = explode(' ', $this->image_border); } if (sizeof($vars) == 4) { $ct = $vars[0]; $cr = $vars[1]; $cb = $vars[2]; $cl = $vars[3]; } else { if (sizeof($vars) == 2) { $ct = $vars[0]; $cr = $vars[1]; $cb = $vars[0]; $cl = $vars[1]; } else { $ct = $vars[0]; $cr = $vars[0]; $cb = $vars[0]; $cl = $vars[0]; } } if (strpos($ct, '%') > 0) { $ct = $this->image_dst_y * (str_replace('%', '', $ct) / 100); } if (strpos($cr, '%') > 0) { $cr = $this->image_dst_x * (str_replace('%', '', $cr) / 100); } if (strpos($cb, '%') > 0) { $cb = $this->image_dst_y * (str_replace('%', '', $cb) / 100); } if (strpos($cl, '%') > 0) { $cl = $this->image_dst_x * (str_replace('%', '', $cl) / 100); } if (strpos($ct, 'px') > 0) { $ct = str_replace('px', '', $ct); } if (strpos($cr, 'px') > 0) { $cr = str_replace('px', '', $cr); } if (strpos($cb, 'px') > 0) { $cb = str_replace('px', '', $cb); } if (strpos($cl, 'px') > 0) { $cl = str_replace('px', '', $cl); } $ct = (int) $ct; $cr = (int) $cr; $cb = (int) $cb; $cl = (int) $cl; $this->image_dst_x = $this->image_dst_x + $cl + $cr; $this->image_dst_y = $this->image_dst_y + $ct + $cb; if (!empty($this->image_border_color)) { list($red, $green, $blue) = $this->getcolors($this->image_border_color); } // we now create an image, that we fill with the border color $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y); $background = imagecolorallocatealpha($tmp, $red, $green, $blue, 0); imagefilledrectangle($tmp, 0, 0, $this->image_dst_x, $this->image_dst_y, $background); // we then copy the source image into the new image, without merging so that only the border is actually kept imagecopy($tmp, $image_dst, $cl, $ct, 0, 0, $this->image_dst_x - $cr - $cl, $this->image_dst_y - $cb - $ct); // we transfert tmp into image_dst $image_dst = $this->imagetransfer($tmp, $image_dst); } // add frame border if (is_numeric($this->image_frame)) { if (is_array($this->image_frame_colors)) { $vars = $this->image_frame_colors; $this->log .= '- add frame : ' . implode(' ', $this->image_frame_colors) . '<br />'; } else { $this->log .= '- add frame : ' . $this->image_frame_colors . '<br />'; $vars = explode(' ', $this->image_frame_colors); } $nb = sizeof($vars); $this->image_dst_x = $this->image_dst_x + $nb * 2; $this->image_dst_y = $this->image_dst_y + $nb * 2; $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y); imagecopy($tmp, $image_dst, $nb, $nb, 0, 0, $this->image_dst_x - $nb * 2, $this->image_dst_y - $nb * 2); for ($i = 0; $i < $nb; $i++) { list($red, $green, $blue) = $this->getcolors($vars[$i]); $c = imagecolorallocate($tmp, $red, $green, $blue); if ($this->image_frame == 1) { imageline($tmp, $i, $i, $this->image_dst_x - $i - 1, $i, $c); imageline($tmp, $this->image_dst_x - $i - 1, $this->image_dst_y - $i - 1, $this->image_dst_x - $i - 1, $i, $c); imageline($tmp, $this->image_dst_x - $i - 1, $this->image_dst_y - $i - 1, $i, $this->image_dst_y - $i - 1, $c); imageline($tmp, $i, $i, $i, $this->image_dst_y - $i - 1, $c); } else { imageline($tmp, $i, $i, $this->image_dst_x - $i - 1, $i, $c); imageline($tmp, $this->image_dst_x - $nb + $i, $this->image_dst_y - $nb + $i, $this->image_dst_x - $nb + $i, $nb - $i, $c); imageline($tmp, $this->image_dst_x - $nb + $i, $this->image_dst_y - $nb + $i, $nb - $i, $this->image_dst_y - $nb + $i, $c); imageline($tmp, $i, $i, $i, $this->image_dst_y - $i - 1, $c); } } // we transfert tmp into image_dst $image_dst = $this->imagetransfer($tmp, $image_dst); } // add bevel border if ($this->image_bevel > 0) { if (empty($this->image_bevel_color1)) { $this->image_bevel_color1 = '#FFFFFF'; } if (empty($this->image_bevel_color2)) { $this->image_bevel_color2 = '#000000'; } list($red1, $green1, $blue1) = $this->getcolors($this->image_bevel_color1); list($red2, $green2, $blue2) = $this->getcolors($this->image_bevel_color2); $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y); imagecopy($tmp, $image_dst, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y); imagealphablending($tmp, true); for ($i = 0; $i < $this->image_bevel; $i++) { $alpha = round($i / $this->image_bevel * 127); $c1 = imagecolorallocatealpha($tmp, $red1, $green1, $blue1, $alpha); $c2 = imagecolorallocatealpha($tmp, $red2, $green2, $blue2, $alpha); imageline($tmp, $i, $i, $this->image_dst_x - $i - 1, $i, $c1); imageline($tmp, $this->image_dst_x - $i - 1, $this->image_dst_y - $i, $this->image_dst_x - $i - 1, $i, $c2); imageline($tmp, $this->image_dst_x - $i - 1, $this->image_dst_y - $i - 1, $i, $this->image_dst_y - $i - 1, $c2); imageline($tmp, $i, $i, $i, $this->image_dst_y - $i - 1, $c1); } // we transfert tmp into image_dst $image_dst = $this->imagetransfer($tmp, $image_dst); } // add watermark image if ($this->image_watermark != '' && file_exists($this->image_watermark)) { $this->log .= '- add watermark<br />'; $this->image_watermark_position = strtolower($this->image_watermark_position); $watermark_info = getimagesize($this->image_watermark); $watermark_type = array_key_exists(2, $watermark_info) ? $watermark_info[2] : null; // 1 = GIF, 2 = JPG, 3 = PNG $watermark_checked = false; if ($watermark_type == IMAGETYPE_GIF) { if (!function_exists('imagecreatefromgif')) { $this->error = $this->translate('watermark_no_create_support', array('GIF')); } else { $filter = @imagecreatefromgif($this->image_watermark); if (!$filter) { $this->error = $this->translate('watermark_create_error', array('GIF')); } else { $this->log .= ' watermark source image is GIF<br />'; $watermark_checked = true; } } } else { if ($watermark_type == IMAGETYPE_JPEG) { if (!function_exists('imagecreatefromjpeg')) { $this->error = $this->translate('watermark_no_create_support', array('JPEG')); } else { $filter = @imagecreatefromjpeg($this->image_watermark); if (!$filter) { $this->error = $this->translate('watermark_create_error', array('JPEG')); } else { $this->log .= ' watermark source image is JPEG<br />'; $watermark_checked = true; } } } else { if ($watermark_type == IMAGETYPE_PNG) { if (!function_exists('imagecreatefrompng')) { $this->error = $this->translate('watermark_no_create_support', array('PNG')); } else { $filter = @imagecreatefrompng($this->image_watermark); if (!$filter) { $this->error = $this->translate('watermark_create_error', array('PNG')); } else { $this->log .= ' watermark source image is PNG<br />'; $watermark_checked = true; } } } else { if ($watermark_type == IMAGETYPE_BMP) { if (!method_exists($this, 'imagecreatefrombmp')) { $this->error = $this->translate('watermark_no_create_support', array('BMP')); } else { $filter = @$this->imagecreatefrombmp($this->image_watermark); if (!$filter) { $this->error = $this->translate('watermark_create_error', array('BMP')); } else { $this->log .= ' watermark source image is BMP<br />'; $watermark_checked = true; } } } else { $this->error = $this->translate('watermark_invalid'); } } } } if ($watermark_checked) { $watermark_dst_width = $watermark_src_width = imagesx($filter); $watermark_dst_height = $watermark_src_height = imagesy($filter); // if watermark is too large/tall, resize it first if (!$this->image_watermark_no_zoom_out && ($watermark_dst_width > $this->image_dst_x || $watermark_dst_height > $this->image_dst_y) || !$this->image_watermark_no_zoom_in && $watermark_dst_width < $this->image_dst_x && $watermark_dst_height < $this->image_dst_y) { $canvas_width = $this->image_dst_x - abs($this->image_watermark_x); $canvas_height = $this->image_dst_y - abs($this->image_watermark_y); if ($watermark_src_width / $canvas_width > $watermark_src_height / $canvas_height) { $watermark_dst_width = $canvas_width; $watermark_dst_height = intval($watermark_src_height * ($canvas_width / $watermark_src_width)); } else { $watermark_dst_height = $canvas_height; $watermark_dst_width = intval($watermark_src_width * ($canvas_height / $watermark_src_height)); } $this->log .= ' watermark resized from ' . $watermark_src_width . 'x' . $watermark_src_height . ' to ' . $watermark_dst_width . 'x' . $watermark_dst_height . '<br />'; } // determine watermark position $watermark_x = 0; $watermark_y = 0; if (is_numeric($this->image_watermark_x)) { if ($this->image_watermark_x < 0) { $watermark_x = $this->image_dst_x - $watermark_dst_width + $this->image_watermark_x; } else { $watermark_x = $this->image_watermark_x; } } else { if (strpos($this->image_watermark_position, 'r') !== false) { $watermark_x = $this->image_dst_x - $watermark_dst_width; } else { if (strpos($this->image_watermark_position, 'l') !== false) { $watermark_x = 0; } else { $watermark_x = ($this->image_dst_x - $watermark_dst_width) / 2; } } } if (is_numeric($this->image_watermark_y)) { if ($this->image_watermark_y < 0) { $watermark_y = $this->image_dst_y - $watermark_dst_height + $this->image_watermark_y; } else { $watermark_y = $this->image_watermark_y; } } else { if (strpos($this->image_watermark_position, 'b') !== false) { $watermark_y = $this->image_dst_y - $watermark_dst_height; } else { if (strpos($this->image_watermark_position, 't') !== false) { $watermark_y = 0; } else { $watermark_y = ($this->image_dst_y - $watermark_dst_height) / 2; } } } imagealphablending($image_dst, true); imagecopyresampled($image_dst, $filter, $watermark_x, $watermark_y, 0, 0, $watermark_dst_width, $watermark_dst_height, $watermark_src_width, $watermark_src_height); } else { $this->error = $this->translate('watermark_invalid'); } } // add text if (!empty($this->image_text)) { $this->log .= '- add text<br />'; // calculate sizes in human readable format $src_size = $this->file_src_size / 1024; $src_size_mb = number_format($src_size / 1024, 1, ".", " "); $src_size_kb = number_format($src_size, 1, ".", " "); $src_size_human = $src_size > 1024 ? $src_size_mb . " MB" : $src_size_kb . " kb"; $this->image_text = str_replace(array('[src_name]', '[src_name_body]', '[src_name_ext]', '[src_pathname]', '[src_mime]', '[src_size]', '[src_size_kb]', '[src_size_mb]', '[src_size_human]', '[src_x]', '[src_y]', '[src_pixels]', '[src_type]', '[src_bits]', '[dst_path]', '[dst_name_body]', '[dst_name_ext]', '[dst_name]', '[dst_pathname]', '[dst_x]', '[dst_y]', '[date]', '[time]', '[host]', '[server]', '[ip]', '[gd_version]'), array($this->file_src_name, $this->file_src_name_body, $this->file_src_name_ext, $this->file_src_pathname, $this->file_src_mime, $this->file_src_size, $src_size_kb, $src_size_mb, $src_size_human, $this->image_src_x, $this->image_src_y, $this->image_src_pixels, $this->image_src_type, $this->image_src_bits, $this->file_dst_path, $this->file_dst_name_body, $this->file_dst_name_ext, $this->file_dst_name, $this->file_dst_pathname, $this->image_dst_x, $this->image_dst_y, date('Y-m-d'), date('H:i:s'), isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'n/a', isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'n/a', isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 'n/a', $this->gdversion(true)), $this->image_text); if (!is_numeric($this->image_text_padding)) { $this->image_text_padding = 0; } if (!is_numeric($this->image_text_line_spacing)) { $this->image_text_line_spacing = 0; } if (!is_numeric($this->image_text_padding_x)) { $this->image_text_padding_x = $this->image_text_padding; } if (!is_numeric($this->image_text_padding_y)) { $this->image_text_padding_y = $this->image_text_padding; } $this->image_text_position = strtolower($this->image_text_position); $this->image_text_direction = strtolower($this->image_text_direction); $this->image_text_alignment = strtolower($this->image_text_alignment); // if the font is a string, we assume that we might want to load a font if (!is_numeric($this->image_text_font) && strlen($this->image_text_font) > 4 && substr(strtolower($this->image_text_font), -4) == '.gdf') { $this->log .= ' try to load font ' . $this->image_text_font . '... '; if ($this->image_text_font = @imageloadfont($this->image_text_font)) { $this->log .= 'success<br />'; } else { $this->log .= 'error<br />'; $this->image_text_font = 5; } } $text = explode("\n", $this->image_text); $char_width = imagefontwidth($this->image_text_font); $char_height = imagefontheight($this->image_text_font); $text_height = 0; $text_width = 0; $line_height = 0; $line_width = 0; foreach ($text as $k => $v) { if ($this->image_text_direction == 'v') { $h = $char_width * strlen($v); if ($h > $text_height) { $text_height = $h; } $line_width = $char_height; $text_width += $line_width + ($k < sizeof($text) - 1 ? $this->image_text_line_spacing : 0); } else { $w = $char_width * strlen($v); if ($w > $text_width) { $text_width = $w; } $line_height = $char_height; $text_height += $line_height + ($k < sizeof($text) - 1 ? $this->image_text_line_spacing : 0); } } $text_width += 2 * $this->image_text_padding_x; $text_height += 2 * $this->image_text_padding_y; $text_x = 0; $text_y = 0; if (is_numeric($this->image_text_x)) { if ($this->image_text_x < 0) { $text_x = $this->image_dst_x - $text_width + $this->image_text_x; } else { $text_x = $this->image_text_x; } } else { if (strpos($this->image_text_position, 'r') !== false) { $text_x = $this->image_dst_x - $text_width; } else { if (strpos($this->image_text_position, 'l') !== false) { $text_x = 0; } else { $text_x = ($this->image_dst_x - $text_width) / 2; } } } if (is_numeric($this->image_text_y)) { if ($this->image_text_y < 0) { $text_y = $this->image_dst_y - $text_height + $this->image_text_y; } else { $text_y = $this->image_text_y; } } else { if (strpos($this->image_text_position, 'b') !== false) { $text_y = $this->image_dst_y - $text_height; } else { if (strpos($this->image_text_position, 't') !== false) { $text_y = 0; } else { $text_y = ($this->image_dst_y - $text_height) / 2; } } } // add a background, maybe transparent if (!empty($this->image_text_background)) { list($red, $green, $blue) = $this->getcolors($this->image_text_background); if ($gd_version >= 2 && is_numeric($this->image_text_background_percent) && $this->image_text_background_percent >= 0 && $this->image_text_background_percent <= 100) { $filter = imagecreatetruecolor($text_width, $text_height); $background_color = imagecolorallocate($filter, $red, $green, $blue); imagefilledrectangle($filter, 0, 0, $text_width, $text_height, $background_color); $this->imagecopymergealpha($image_dst, $filter, $text_x, $text_y, 0, 0, $text_width, $text_height, $this->image_text_background_percent); imagedestroy($filter); } else { $background_color = imagecolorallocate($image_dst, $red, $green, $blue); imagefilledrectangle($image_dst, $text_x, $text_y, $text_x + $text_width, $text_y + $text_height, $background_color); } } $text_x += $this->image_text_padding_x; $text_y += $this->image_text_padding_y; $t_width = $text_width - 2 * $this->image_text_padding_x; $t_height = $text_height - 2 * $this->image_text_padding_y; list($red, $green, $blue) = $this->getcolors($this->image_text_color); // add the text, maybe transparent if ($gd_version >= 2 && is_numeric($this->image_text_percent) && $this->image_text_percent >= 0 && $this->image_text_percent <= 100) { if ($t_width < 0) { $t_width = 0; } if ($t_height < 0) { $t_height = 0; } $filter = $this->imagecreatenew($t_width, $t_height, false, true); $text_color = imagecolorallocate($filter, $red, $green, $blue); foreach ($text as $k => $v) { if ($this->image_text_direction == 'v') { imagestringup($filter, $this->image_text_font, $k * ($line_width + ($k > 0 && $k < sizeof($text) ? $this->image_text_line_spacing : 0)), $text_height - 2 * $this->image_text_padding_y - ($this->image_text_alignment == 'l' ? 0 : ($t_height - strlen($v) * $char_width) / ($this->image_text_alignment == 'r' ? 1 : 2)), $v, $text_color); } else { imagestring($filter, $this->image_text_font, $this->image_text_alignment == 'l' ? 0 : ($t_width - strlen($v) * $char_width) / ($this->image_text_alignment == 'r' ? 1 : 2), $k * ($line_height + ($k > 0 && $k < sizeof($text) ? $this->image_text_line_spacing : 0)), $v, $text_color); } } $this->imagecopymergealpha($image_dst, $filter, $text_x, $text_y, 0, 0, $t_width, $t_height, $this->image_text_percent); imagedestroy($filter); } else { $text_color = imageColorAllocate($image_dst, $red, $green, $blue); foreach ($text as $k => $v) { if ($this->image_text_direction == 'v') { imagestringup($image_dst, $this->image_text_font, $text_x + $k * ($line_width + ($k > 0 && $k < sizeof($text) ? $this->image_text_line_spacing : 0)), $text_y + $text_height - 2 * $this->image_text_padding_y - ($this->image_text_alignment == 'l' ? 0 : ($t_height - strlen($v) * $char_width) / ($this->image_text_alignment == 'r' ? 1 : 2)), $v, $text_color); } else { imagestring($image_dst, $this->image_text_font, $text_x + ($this->image_text_alignment == 'l' ? 0 : ($t_width - strlen($v) * $char_width) / ($this->image_text_alignment == 'r' ? 1 : 2)), $text_y + $k * ($line_height + ($k > 0 && $k < sizeof($text) ? $this->image_text_line_spacing : 0)), $v, $text_color); } } } } // add a reflection if ($this->image_reflection_height) { $this->log .= '- add reflection : ' . $this->image_reflection_height . '<br />'; // we decode image_reflection_height, which can be a integer, a string in pixels or percentage $image_reflection_height = $this->image_reflection_height; if (strpos($image_reflection_height, '%') > 0) { $image_reflection_height = $this->image_dst_y * str_replace('%', '', $image_reflection_height / 100); } if (strpos($image_reflection_height, 'px') > 0) { $image_reflection_height = str_replace('px', '', $image_reflection_height); } $image_reflection_height = (int) $image_reflection_height; if ($image_reflection_height > $this->image_dst_y) { $image_reflection_height = $this->image_dst_y; } if (empty($this->image_reflection_opacity)) { $this->image_reflection_opacity = 60; } // create the new destination image $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y + $image_reflection_height + $this->image_reflection_space, true); $transparency = $this->image_reflection_opacity; // copy the original image imagecopy($tmp, $image_dst, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y + ($this->image_reflection_space < 0 ? $this->image_reflection_space : 0)); // we have to make sure the extra bit is the right color, or transparent if ($image_reflection_height + $this->image_reflection_space > 0) { // use the background color if present if (!empty($this->image_background_color)) { list($red, $green, $blue) = $this->getcolors($this->image_background_color); $fill = imagecolorallocate($tmp, $red, $green, $blue); } else { $fill = imagecolorallocatealpha($tmp, 0, 0, 0, 127); } // fill in from the edge of the extra bit imagefill($tmp, round($this->image_dst_x / 2), $this->image_dst_y + $image_reflection_height + $this->image_reflection_space - 1, $fill); } // copy the reflection for ($y = 0; $y < $image_reflection_height; $y++) { for ($x = 0; $x < $this->image_dst_x; $x++) { $pixel_b = imagecolorsforindex($tmp, imagecolorat($tmp, $x, $y + $this->image_dst_y + $this->image_reflection_space)); $pixel_o = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $this->image_dst_y - $y - 1 + ($this->image_reflection_space < 0 ? $this->image_reflection_space : 0))); $alpha_o = 1 - $pixel_o['alpha'] / 127; $alpha_b = 1 - $pixel_b['alpha'] / 127; $opacity = $alpha_o * $transparency / 100; if ($opacity > 0) { $red = round(($pixel_o['red'] * $opacity + $pixel_b['red'] * $alpha_b) / ($alpha_b + $opacity)); $green = round(($pixel_o['green'] * $opacity + $pixel_b['green'] * $alpha_b) / ($alpha_b + $opacity)); $blue = round(($pixel_o['blue'] * $opacity + $pixel_b['blue'] * $alpha_b) / ($alpha_b + $opacity)); $alpha = $opacity + $alpha_b; if ($alpha > 1) { $alpha = 1; } $alpha = round((1 - $alpha) * 127); $color = imagecolorallocatealpha($tmp, $red, $green, $blue, $alpha); imagesetpixel($tmp, $x, $y + $this->image_dst_y + $this->image_reflection_space, $color); } } if ($transparency > 0) { $transparency = $transparency - $this->image_reflection_opacity / $image_reflection_height; } } // copy the resulting image into the destination image $this->image_dst_y = $this->image_dst_y + $image_reflection_height + $this->image_reflection_space; $image_dst = $this->imagetransfer($tmp, $image_dst); } // reduce the JPEG image to a set desired size if (is_numeric($this->jpeg_size) && $this->jpeg_size > 0 && ($this->image_convert == 'jpeg' || $this->image_convert == 'jpg')) { // inspired by: JPEGReducer class version 1, 25 November 2004, Author: Huda M ElMatsani, justhuda at netscape dot net $this->log .= '- JPEG desired file size : ' . $this->jpeg_size . '<br />'; // calculate size of each image. 75%, 50%, and 25% quality ob_start(); imagejpeg($image_dst, '', 75); $buffer = ob_get_contents(); ob_end_clean(); $size75 = strlen($buffer); ob_start(); imagejpeg($image_dst, '', 50); $buffer = ob_get_contents(); ob_end_clean(); $size50 = strlen($buffer); ob_start(); imagejpeg($image_dst, '', 25); $buffer = ob_get_contents(); ob_end_clean(); $size25 = strlen($buffer); // calculate gradient of size reduction by quality $mgrad1 = 25 / ($size50 - $size25); $mgrad2 = 25 / ($size75 - $size50); $mgrad3 = 50 / ($size75 - $size25); $mgrad = ($mgrad1 + $mgrad2 + $mgrad3) / 3; // result of approx. quality factor for expected size $q_factor = round($mgrad * ($this->jpeg_size - $size50) + 50); if ($q_factor < 1) { $this->jpeg_quality = 1; } elseif ($q_factor > 100) { $this->jpeg_quality = 100; } else { $this->jpeg_quality = $q_factor; } $this->log .= ' JPEG quality factor set to ' . $this->jpeg_quality . '<br />'; } // converts image from true color, and fix transparency if needed $this->log .= '- converting...<br />'; switch ($this->image_convert) { case 'gif': // if the image is true color, we convert it to a palette if (imageistruecolor($image_dst)) { $this->log .= ' true color to palette<br />'; // creates a black and white mask $mask = array(array()); for ($x = 0; $x < $this->image_dst_x; $x++) { for ($y = 0; $y < $this->image_dst_y; $y++) { $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y)); $mask[$x][$y] = $pixel['alpha']; } } list($red, $green, $blue) = $this->getcolors($this->image_default_color); // first, we merge the image with the background color, so we know which colors we will have for ($x = 0; $x < $this->image_dst_x; $x++) { for ($y = 0; $y < $this->image_dst_y; $y++) { if ($mask[$x][$y] > 0) { // we have some transparency. we combine the color with the default color $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y)); $alpha = $mask[$x][$y] / 127; $pixel['red'] = round($pixel['red'] * (1 - $alpha) + $red * $alpha); $pixel['green'] = round($pixel['green'] * (1 - $alpha) + $green * $alpha); $pixel['blue'] = round($pixel['blue'] * (1 - $alpha) + $blue * $alpha); $color = imagecolorallocate($image_dst, $pixel['red'], $pixel['green'], $pixel['blue']); imagesetpixel($image_dst, $x, $y, $color); } } } // transforms the true color image into palette, with its merged default color if (empty($this->image_background_color)) { imagetruecolortopalette($image_dst, true, 255); $transparency = imagecolorallocate($image_dst, 254, 1, 253); imagecolortransparent($image_dst, $transparency); // make the transparent areas transparent for ($x = 0; $x < $this->image_dst_x; $x++) { for ($y = 0; $y < $this->image_dst_y; $y++) { // we test wether we have enough opacity to justify keeping the color if ($mask[$x][$y] > 120) { imagesetpixel($image_dst, $x, $y, $transparency); } } } } unset($mask); } break; case 'jpg': case 'bmp': // if the image doesn't support any transparency, then we merge it with the default color $this->log .= ' fills in transparency with default color<br />'; list($red, $green, $blue) = $this->getcolors($this->image_default_color); $transparency = imagecolorallocate($image_dst, $red, $green, $blue); // make the transaparent areas transparent for ($x = 0; $x < $this->image_dst_x; $x++) { for ($y = 0; $y < $this->image_dst_y; $y++) { // we test wether we have some transparency, in which case we will merge the colors if (imageistruecolor($image_dst)) { $rgba = imagecolorat($image_dst, $x, $y); $pixel = array('red' => $rgba >> 16 & 0xff, 'green' => $rgba >> 8 & 0xff, 'blue' => $rgba & 0xff, 'alpha' => ($rgba & 0x7f000000) >> 24); } else { $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y)); } if ($pixel['alpha'] == 127) { // we have full transparency. we make the pixel transparent imagesetpixel($image_dst, $x, $y, $transparency); } else { if ($pixel['alpha'] > 0) { // we have some transparency. we combine the color with the default color $alpha = $pixel['alpha'] / 127; $pixel['red'] = round($pixel['red'] * (1 - $alpha) + $red * $alpha); $pixel['green'] = round($pixel['green'] * (1 - $alpha) + $green * $alpha); $pixel['blue'] = round($pixel['blue'] * (1 - $alpha) + $blue * $alpha); $color = imagecolorclosest($image_dst, $pixel['red'], $pixel['green'], $pixel['blue']); imagesetpixel($image_dst, $x, $y, $color); } } } } break; default: break; } // outputs image $this->log .= '- saving image...<br />'; switch ($this->image_convert) { case 'jpeg': case 'jpg': if (!$return_mode) { $result = @imagejpeg($image_dst, $this->file_dst_pathname, $this->jpeg_quality); } else { ob_start(); $result = @imagejpeg($image_dst, '', $this->jpeg_quality); $return_content = ob_get_contents(); ob_end_clean(); } if (!$result) { $this->processed = false; $this->error = $this->translate('file_create', array('JPEG')); } else { $this->log .= ' JPEG image created<br />'; } break; case 'png': imagealphablending($image_dst, false); imagesavealpha($image_dst, true); if (!$return_mode) { $result = @imagepng($image_dst, $this->file_dst_pathname); } else { ob_start(); $result = @imagepng($image_dst); $return_content = ob_get_contents(); ob_end_clean(); } if (!$result) { $this->processed = false; $this->error = $this->translate('file_create', array('PNG')); } else { $this->log .= ' PNG image created<br />'; } break; case 'gif': if (!$return_mode) { $result = @imagegif($image_dst, $this->file_dst_pathname); } else { ob_start(); $result = @imagegif($image_dst); $return_content = ob_get_contents(); ob_end_clean(); } if (!$result) { $this->processed = false; $this->error = $this->translate('file_create', array('GIF')); } else { $this->log .= ' GIF image created<br />'; } break; case 'bmp': if (!$return_mode) { $result = $this->imagebmp($image_dst, $this->file_dst_pathname); } else { ob_start(); $result = $this->imagebmp($image_dst); $return_content = ob_get_contents(); ob_end_clean(); } if (!$result) { $this->processed = false; $this->error = $this->translate('file_create', array('BMP')); } else { $this->log .= ' BMP image created<br />'; } break; default: $this->processed = false; $this->error = $this->translate('no_conversion_type'); } if ($this->processed) { if (is_resource($image_src)) { imagedestroy($image_src); } if (is_resource($image_dst)) { imagedestroy($image_dst); } $this->log .= ' image objects destroyed<br />'; } } } else { $this->log .= '- no image processing wanted<br />'; if (!$return_mode) { // copy the file to its final destination. we don't use move_uploaded_file here // if we happen to have open_basedir restrictions, it is a temp file that we copy, not the original uploaded file if (!copy($this->file_src_pathname, $this->file_dst_pathname)) { $this->processed = false; $this->error = $this->translate('copy_failed'); } } else { // returns the file, so that its content can be received by the caller $return_content = @file_get_contents($this->file_src_pathname); if ($return_content === FALSE) { $this->processed = false; $this->error = $this->translate('reading_failed'); } } } } if ($this->processed) { $this->log .= '- <b>process OK</b><br />'; } else { $this->log .= '- <b>error</b>: ' . $this->error . '<br />'; } // we reinit all the vars $this->init(); // we may return the image content if ($return_mode) { return $return_content; } }
/** * main class method; Create the image and return the url to the image file; * @param string relative path to site root * @return mixt image url or error */ function getImageURL($relpath) { $this->garbageCollector(); // check write permissions on captcha folder $fld = new KT_folder(); $fld->createFolder(KT_CAPTCHA_TEMP_FOLDER); if (!$fld->checkRights(KT_CAPTCHA_TEMP_FOLDER, 'write')) { if ($GLOBALS['tNG_debug_mode'] == 'DEVELOPMENT') { $error = KT_getResource('FOLDER_ERROR_D', 'Captcha', array(KT_CAPTCHA_TEMP_FOLDER)); } else { $error = KT_getResource('FOLDER_ERROR', 'Captcha'); } return $this->formatError($error); } // with gd if ($this->lib == "gd") { $arr["GD Version"] = 'GD not available'; if (function_exists('gd_info')) { $arr = gd_info(); preg_match("/(2)\\.[\\d]+/i", $arr["GD Version"], $matches); } if (!isset($arr) || !isset($matches[1]) || (int) $matches[1] < 2) { $error = KT_getResource('PHP_GD_VERSION_ERROR', 'Captcha', array($arr["GD Version"])); return $this->formatError($error); } $im = imagecreatefrompng(dirname(__FILE__) . '/captcha.png') or $error = KT_getResource('PHP_GD_ERROR', 'Captcha', array()); if (isset($error)) { return $this->formatError($error); } $string = $this->getTextCaptcha(); $font = imageloadfont(dirname(__FILE__) . "/fonts/Courier.gdf"); if ($font === false) { $font = 5; } $fontFileName = dirname(__FILE__) . '/fonts/MyriadWebPro.ttf'; $wFont = 24; $hFont = 24; // write the letters for ($i = 0; $i < strlen($string); $i++) { $color1 = rand(0, 64); $color2 = rand(0, 64); $color3 = rand(0, 64); $text_color = imagecolorallocate($im, $color1, $color2, $color3); $okttf = false; if (function_exists('imagettftext')) { $okttf = @imagettftext($im, 14, rand(-25, 25), 10 + $i * $wFont, $hFont + rand(4, 26), $text_color, $fontFileName, $string[$i]); } if ($okttf === false) { $fim = imagecreatetruecolor($wFont + 9, $hFont + 9); $back = imagecolorallocate($fim, 255, 255, 255); imagefilledrectangle($fim, 0, 0, $wFont + 8, $hFont + 8, $back); $transparent2 = imagecolorallocate($fim, 255, 255, 255); $text_color = imagecolorallocate($fim, $color1, $color2, $color3); imagestring($fim, $font, 4, 4, $string[$i], $text_color); if (function_exists("imagerotate")) { $fim = imagerotate($fim, rand(-25, 25), $transparent2); } $iTranspa2 = imagecolortransparent($fim, $transparent2); imagecopymerge($im, $fim, 0 + $i * $wFont, rand(4, 26), 0, 0, $wFont + 9, $hFont + 9, 80); imagedestroy($fim); } } imagepng($im, KT_CAPTCHA_TEMP_FOLDER . $this->filename); imagedestroy($im); // with imagemagick } else { $sourceFileName = dirname(__FILE__) . '/captcha.png'; $fontFileName = dirname(__FILE__) . '/fonts/MyriadWebPro.ttf'; $destFileName = KT_CAPTCHA_TEMP_FOLDER . $this->filename; $arrCommands = array($GLOBALS['KT_prefered_imagemagick_path'] . 'convert'); $shell = new KT_shell(); $direction = rand(0, 10); if ($direction % 2 == 0) { $textRend = -rand(8, 11) . 'x0+' . (5 + (8 - strlen($this->text)) * 20) . '+' . (70 - (8 - strlen($this->text)) * 5); } else { $textRend = rand(8, 11) . 'x0+' . (5 + (8 - strlen($this->text)) * 20) . '+' . (35 + (8 - strlen($this->text)) * 5); } $arrArguments = array('-font', $fontFileName, '-pointsize', '34', '-fill', 'rgb(' . rand(0, 32) . ',' . rand(0, 32) . ',' . rand(0, 32) . ')', '-annotate', $textRend, $this->text, '-wave', '3x50', '-region', '100x70+' . rand(0, 100) . '+0', '-swirl', '25', '-region', '100x70+' . rand(0, 100) . '+0', '-swirl', '-25', $sourceFileName, $destFileName); $shell->execute($arrCommands, $arrArguments); if ($shell->hasError()) { $arr = $shell->getError(); $ret = $this->formatError($arr[0]); return $ret; } } return $relpath . KT_CAPTCHA_TEMP_URL . $this->filename; }
/** * Draw the CAPTCHA code over the image * * @access private * */ function drawWord() { $width2 = $this->image_width * $this->iscale; $height2 = $this->image_height * $this->iscale; $text_color = $this->text_color; $gd_info = gd_info(); if ($this->use_gd_font == true || !function_exists('imagettftext') || $gd_info['FreeType Support'] == false) { $this->gd_font_file = $this->getFontFromDirectory($this->gd_font_directory, 'gdf'); if (!is_int($this->gd_font_file)) { //is a file name $font = @imageloadfont($this->gd_font_file); if ($font == false) { trigger_error("Failed to load GD Font file {$this->gd_font_file} ", E_USER_WARNING); return; } } else { //gd font identifier $font = $this->gd_font_file; } $color = imagecolorallocate($this->im, hexdec(substr($text_color, 1, 2)), hexdec(substr($text_color, 3, 2)), hexdec(substr($text_color, 5, 2))); imagestring($this->im, $font, $this->text_x_start, $this->image_height / 2 - $this->gd_font_size / 2, $this->code, $color); } else { //ttf font $this->ttf_file = $this->getFontFromDirectory($this->ttf_font_directory, 'ttf'); $text_color = $this->getColorArray($this->text_color, '#3d3d3d'); $font_size = $height2 * 0.6; // was .35 but fonts wre too small $bb = imagettfbbox($font_size, 0, $this->ttf_file, $this->code); // repeat this line to fix random missing text on some Debian servers $bb = imagettfbbox($font_size, 0, $this->ttf_file, $this->code); $tx = $bb[4] - $bb[0]; $ty = $bb[5] - $bb[1]; $x = floor($width2 / 2 - $tx / 2 - $bb[0]); $y = round($height2 / 2 - $ty / 2 - $bb[1]); if ($this->use_transparent_text == true) { $alpha = intval($this->text_transparency_percentage / 100 * 127); $font_color = imagecolorallocatealpha($this->tmpimg, $text_color[0], $text_color[1], $text_color[2], $alpha); } else { //no transparency $font_color = imagecolorallocate($this->tmpimg, $text_color[0], $text_color[1], $text_color[2]); } $strlen = strlen($this->code); if (!is_array($this->multi_text_color)) { $this->use_multi_text = false; } if ($this->use_multi_text == false && $this->text_angle_minimum == 0 && $this->text_angle_maximum == 0) { // no angled or multi-color characters imagettftext($this->tmpimg, $font_size, 0, $x, $y, $font_color, $this->ttf_file, $this->code); } else { $this->multi_text_color = $this->convertMultiTextColor($this->multi_text_color); for ($i = 0; $i < $strlen; ++$i) { $angle = rand($this->text_angle_minimum, $this->text_angle_maximum); $y = rand($y - 2, $y + 2); // up/down align was 5 , but diff was too steep $x = $x - 5; // left placement if ($this->use_multi_text == true) { $idx = rand(0, sizeof($this->multi_text_color) - 1); if ($this->use_transparent_text == true) { $font_color = imagecolorallocatealpha($this->tmpimg, $this->multi_text_color[$idx][0], $this->multi_text_color[$idx][1], $this->multi_text_color[$idx][2], $alpha); } else { $font_color = imagecolorallocate($this->tmpimg, $this->multi_text_color[$idx][0], $this->multi_text_color[$idx][1], $this->multi_text_color[$idx][2]); } } $ch = $this->code[$i]; imagettftext($this->tmpimg, $font_size, $angle, $x, $y, $font_color, $this->ttf_file, $ch); // estimate character widths to increment $x without creating spaces that are too large or too small // these are best estimates to align text but may vary between fonts // for optimal character widths, do not use multiple text colors or character angles and the complete string will be written by imagettftext if (strpos('abcdeghknopqsuvxyz', $ch) !== false) { $min_x = $font_size - $this->iscale * 6; $max_x = $font_size - $this->iscale * 6; } else { if (strpos('ilI1', $ch) !== false) { $min_x = $font_size / 5; $max_x = $font_size / 3; } else { if (strpos('fjrt', $ch) !== false) { $min_x = $font_size - $this->iscale * 12; $max_x = $font_size - $this->iscale * 12; } else { if ($ch == 'wm') { $min_x = $font_size; $max_x = $font_size + $this->iscale * 3; } else { // numbers, capitals or unicode $min_x = $font_size + $this->iscale * 2; $max_x = $font_size + $this->iscale * 5; } } } } $x += rand($min_x, $max_x); } //for loop } // angled or multi-color } //else ttf font //$this->im = $this->tmpimg; //$this->output(); }