private function createnewpicture() { $local = $this->path_two . $this->file['name']; move_uploaded_file($this->file['tmp_name'], $local); $filename = $this->file['tmp_name'] . "/" . $this->file['name']; $this->location = $this->path . "/" . $this->file['name']; switch ($this->file['type']) { case 'image/jpeg': $src = imagecreatefromjpeg($local); break; case 'image/png': $src = imagecreatefrompng($local); break; case 'image/gif': $src = imagecreatefromgif($local); break; default: break; } $sx = imagesx($src); $sy = imagesy($src); $new_image = imagecreatetruecolor($this->newwidth, $this->newheight); if (imagecopyresized($new_image, $src, 0, 0, 0, 0, $this->newwidth, $this->newheight, $sx, $sy)) { if (ImageJpeg($new_image, $this->location) || Imagegif($new_image, $this->location) || Imagepng($new_image, $this->location)) { //imagejpeg($this->location); return true; } } else { return false; } }
public function createBarCode($image_type = 'png', $file_name = null) { $this->parseCode(); $this->image = ImageCreate($this->width + 2 * $this->quiet_zone, $this->height + $this->font_height + $this->quiet_zone); $this->bgcolor = imagecolorallocate($this->image, $this->bgcolor >> 16, $this->bgcolor >> 8 & 0xff, $this->bgcolor & 0xff); $this->color = imagecolorallocate($this->image, $this->color >> 16, $this->color >> 8 & 0xff, $this->color & 0xff); ImageFilledRectangle($this->image, 0, 0, $this->width + 2 * $this->quiet_zone, $this->height + $this->font_height, $this->bgcolor); $sx = $this->quiet_zone; $sy = $this->font_height - 1; $fw = 10; //編號為2或3的字體的寬度為10,為4或5的字體寬度為11 if ($this->font_type > 3) { $sy++; $fw = 11; } $ex = 0; if ($this->text) { //不显示号码,则生成满的条码 if ($this->showType == 'top') { $ey = $this->height + $this->font_height - 2; //条码显示上方 } else { if ($this->showType == 'bottom') { $ey = $this->height - 2; //条码显示下方 $sy = $this->quiet_zone; } } } else { $ey = $this->height + $this->font_height; $sy = $this->quiet_zone; } for ($i = 0; $i < strlen($this->bin_code); $i++) { $ex = $sx + $this->unit_width * (int) $this->bin_code[$i] - 1; if ($i % 2 == 0) { ImageFilledRectangle($this->image, $sx, $sy, $ex, $ey, $this->color); } $sx = $ex + 1; } if ($this->text) { //存在条码号再显示 $t_num = strlen($this->text); $t_x = $this->width / $t_num; $t_sx = ($t_x - $fw) / 2; //目的为了使文字居中平均分布 if ($this->showType == 'top') { $y = 0; } else { if ($this->showType == 'bottom') { $y = $this->height; } } for ($i = 0; $i < $t_num; $i++) { imagechar($this->image, $this->font_type, 6 * $this->unit_width + $t_sx + $i * $t_x, $y, $this->text[$i], $this->color); } } if (!$file_name) { header("Content-Type: image/" . $image_type); } switch ($image_type) { case 'jpg': case 'jpeg': Imagejpeg($this->image, $file_name); break; case 'png': Imagepng($this->image, $file_name); break; case 'gif': break; Imagegif($this->image, $file_name); default: Imagepng($this->image, $file_name); break; } }
public function getCode() { Header("Content-type: image/png"); /* * 初始化 */ $border = 1; //是否要边框 1要:0不要 $how = 4; //验证码位数 $w = $how * 15; //图片宽度 $h = 20; //图片高度 $fontsize = 6; //字体大小 $alpha = "abcdefghijkmnopqrstuvwxyz"; //验证码内容1:字母 $number = "023456789"; //验证码内容2:数字 $randcode = ""; //验证码字符串初始化 srand((double) microtime() * 1000000); //初始化随机数种子 $im = ImageCreate($w, $h); //创建验证图片 /* * 绘制基本框架 */ $bgcolor = ImageColorAllocate($im, 255, 255, 255); //设置背景颜色 ImageFill($im, 0, 0, $bgcolor); //填充背景色 if ($border) { $black = ImageColorAllocate($im, 0, 0, 0); //设置边框颜色 ImageRectangle($im, 0, 0, $w - 1, $h - 1, $black); //绘制边框 } /* * 逐位产生随机字符 */ for ($i = 0; $i < $how; $i++) { $alpha_or_number = mt_rand(0, 1); //字母还是数字 $str = $alpha_or_number ? $alpha : $number; $which = mt_rand(0, strlen($str) - 1); //取哪个字符 $code = substr($str, $which, 1); //取字符 $j = !$i ? 4 : $j + 15; //绘字符位置 $color3 = ImageColorAllocate($im, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100)); //字符随即颜色 ImageChar($im, $fontsize, $j, 3, $code, $color3); //绘字符 $randcode .= $code; //逐位加入验证码字符串 } //把验证码字符串写入session session_start(); $_SESSION['authnum_session'] = $randcode; /* * 添加干扰 */ for ($i = 0; $i < 1; $i++) { $color1 = ImageColorAllocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); //干扰线颜色 ImageArc($im, mt_rand(-5, $w), mt_rand(-5, $h), mt_rand(20, 300), mt_rand(20, 200), 55, 44, $color1); //干扰线 } for ($i = 0; $i < $how * 40; $i++) { $color2 = ImageColorAllocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); //干扰点颜色 ImageSetPixel($im, mt_rand(0, $w), mt_rand(0, $h), $color2); //干扰点 } //关键代码,防止出现'图像因其本身有错无法显示'的问题 ob_clean(); /*绘图结束*/ Imagegif($im); ImageDestroy($im); /*绘图结束*/ }
fwrite($fp, $ob_contents); fclose($fp); ob_end_flush(); } } if (substr($_GET['img'], -3) == "gif") { header("Content-type: image/gif"); if (file_exists($path)) { echo file_get_contents($path); } else { $dst_img = ImageCreate($new_w, $new_h); $src_img = ImageCreateFromGif($_image_); ImagePaletteCopy($dst_img, $src_img); ImageCopyResized($dst_img, $src_img, 0, 0, 0, 0, $new_w, $new_h, ImageSX($src_img), ImageSY($src_img)); ob_start(); $img = Imagegif($dst_img, '', $_quality_); $ob_contents = ob_get_contents(); // Save file $fp = fopen("{$path}", 'wb'); fwrite($fp, $ob_contents); fclose($fp); ob_end_flush(); } } if (substr($_GET['img'], -3) == "png") { header("Content-type: image/png"); if (file_exists($path)) { echo file_get_contents($path); } else { if (strnatcmp(phpversion(), '4.3.0') > 0) { $_quality_ = (int) ($_quality_ / 10);
function show() { Header("Content-type: image/gif"); /* * ��ʼ�� */ $border = 0; //�Ƿ�Ҫ�߿� 1Ҫ:0��Ҫ $how = $this->num; //��֤��λ�� $w = $this->width; //ͼƬ��� $h = $this->height; //ͼƬ�߶� $fontsize = 5; //�����С $alpha = "abcdefghijkmnopqrstuvwxyz"; //��֤������1:��ĸ $number = "023456789"; //��֤������2:���� $randcode = ""; //��֤���ַ�����ʼ�� srand((double) microtime() * 1000000); //��ʼ����������� $im = ImageCreate($w, $h); //������֤ͼƬ /* * ���ƻ������ */ $bgcolor = ImageColorAllocate($im, 255, 255, 255); //���ñ�����ɫ ImageFill($im, 0, 0, $bgcolor); //��䱳��ɫ if ($border) { $black = ImageColorAllocate($im, 0, 0, 0); //���ñ߿���ɫ ImageRectangle($im, 0, 0, $w - 1, $h - 1, $black); //���Ʊ߿� } /* * ��λ��������ַ� */ for ($i = 0; $i < $how; $i++) { $alpha_or_number = mt_rand(0, 1); //��ĸ�������� $str = $alpha_or_number ? $alpha : $number; $which = mt_rand(0, strlen($str) - 1); //ȡ�ĸ��ַ� $code = substr($str, $which, 1); //ȡ�ַ� $j = !$i ? 4 : $j + 15; //���ַ�λ�� $color3 = ImageColorAllocate($im, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100)); //�ַ��漴��ɫ ImageChar($im, $fontsize, $j, 3, $code, $color3); //���ַ� $randcode .= $code; //��λ������֤���ַ��� } /* * ��Ӹ��� */ for ($i = 0; $i < 5; $i++) { $color1 = ImageColorAllocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); //��������ɫ ImageArc($im, mt_rand(-5, $w), mt_rand(-5, $h), mt_rand(20, 300), mt_rand(20, 200), 55, 44, $color1); //������ } for ($i = 0; $i < $how * 15; $i++) { $color2 = ImageColorAllocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); //���ŵ���ɫ ImageSetPixel($im, mt_rand(0, $w), mt_rand(0, $h), $color2); //���ŵ� } //����֤���ַ���д��session //$this->session->set_userdata(array($this->name=>$randcode)); $_SESSION[$this->name] = $randcode; /*��ͼ����*/ Imagegif($im); ImageDestroy($im); /*��ͼ����*/ }
srand((double) microtime() * 1000000); $im = ImageCreate($w, $h); $bgcolor = ImageColorAllocate($im, 255, 255, 255); ImageFill($im, 0, 0, $bgcolor); if ($border) { $black = ImageColorAllocate($im, 0, 0, 0); ImageRectangle($im, 0, 0, $w - 1, $h - 1, $black); } for ($i = 0; $i < $how; $i++) { $alpha_or_number = mt_rand(0, 1); $str = $alpha_or_number ? $alpha : $number; $which = mt_rand(0, strlen($str) - 1); $code = substr($str, $which, 1); $j = !$i ? 4 : $j + 15; $color3 = ImageColorAllocate($im, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100)); ImageChar($im, $fontsize, $j, 3, $code, $color3); $randcode .= $code; } for ($i = 0; $i < 5; $i++) { $color1 = ImageColorAllocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); ImageArc($im, mt_rand(-5, $w), mt_rand(-5, $h), mt_rand(20, 300), mt_rand(20, 200), 55, 44, $color1); } for ($i = 0; $i < $how * 40; $i++) { $color2 = ImageColorAllocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); ImageSetPixel($im, mt_rand(0, $w), mt_rand(0, $h), $color2); } $_SESSION['randcode'] = $randcode; Imagegif($im); ImageDestroy($im); ?>
/** * 生产验证码 */ function show() { /* * 初始化 */ $randcode = ""; //验证码字符串初始化 $im = ImageCreate($this->width, $this->height); //创建验证图片 /* * 绘制基本框架 */ $bgcolor = ImageColorAllocate($im, 255, 255, 255); //设置背景颜色 ImageFill($im, 0, 0, $bgcolor); //填充背景色 if ($this->border) { $black = ImageColorAllocate($im, 0, 0, 0); //设置边框颜色 ImageRectangle($im, 0, 0, $w - 1, $h - 1, $black); //绘制边框 } /* * 逐位产生随机字符 */ for ($i = 0; $i < $this->num; $i++) { $alpha_or_number = mt_rand(0, 1); //字母还是数字 $str = $alpha_or_number ? $this->alpha : $this->number; $which = mt_rand(0, strlen($str) - 1); //取哪个字符 $code = substr($str, $which, 1); //取字符 $j = !$i ? 4 : $j + 15; //绘字符位置 $color3 = ImageColorAllocate($im, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100)); //字符随即颜色 imagettftext($im, $this->fontsize, 0, $j, $this->fontsize * 1.5, $color3, $this->fontpath, $code); //绘字符 $randcode .= $code; //逐位加入验证码字符串 } /* * 添加干扰 */ for ($i = 0; $i < 5; $i++) { $color1 = ImageColorAllocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); //干扰线颜色 ImageArc($im, mt_rand(-5, $this->width), mt_rand(-5, $this->height), mt_rand(20, 300), mt_rand(20, 200), 55, 44, $color1); //干扰线 } for ($i = 0; $i < $this->num * 15; $i++) { $color2 = ImageColorAllocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); //干扰点颜色 ImageSetPixel($im, mt_rand(0, $this->width), mt_rand(0, $this->height), $color2); //干扰点 } // 保存验证码 $key = $this->authcode($this->seKey); $code = $this->authcode(strtoupper($randcode)); $secode = array(); $secode['verify_code'] = $code; // 把校验码保存到session $secode['verify_time'] = $this->now; // 验证码创建时间 $_SESSION[$key] = $secode; /*绘图结束*/ Header("Content-type: image/gif"); Imagegif($im); ImageDestroy($im); /*绘图结束*/ }
function imageResize() { global $engine_path, $ImageTrimX, $ImageTrimY; $source = $this->image; $newWidth = $this->newWidth; $newHeight = $this->newHeight; $imType = $this->type; if ($imType == "jpeg" || $imType == "pjpeg") { $srcImage = ImageCreateFromJPEG($source); } elseif ($imType == "gif") { $srcImage = LoadGif($source); } else { $srcImage = ImageCreateFromPNG($source); } $srcWidth = ImageSX($srcImage); $srcHeight = ImageSY($srcImage); $ratioWidth = $srcWidth / $newWidth; $ratioHeight = $srcHeight / $newHeight; if ($ratioWidth < 1 && $this->fix == "width" || $ratioHeight < 1 && $this->fix == "height") { $destWidth = $srcWidth; $destHeight = $srcHeight; //print "mode 1"; //exit; } elseif ($ratioWidth > $ratioHeight || $ratioWidth < $ratioHeight && $this->fix != "width" || $this->fix == "height") { $destWidth = $srcWidth / $ratioHeight; $destHeight = $newHeight; //print "mode 2"; //exit; } else { $destWidth = $newWidth; $destHeight = $srcHeight / $ratioWidth; //print "mode 3 $ratioHeight $ratioWidth $this->fix"; //exit; } //$this->newWidth=$destWidth; //$this->newHeight=$desHeight; //print "$this->newWidth - $this->newHeight"; //print $srcImage." $ratioWidth $ratioHeight $this->fix $destWidth $destHeight"; //print "-($newHeight-$destHeight)/2"; //exit; if ($newWidth != $srcWidth && $newHeight != $srcHeight && $newWidth != 160) { if (!strlen($ImageTrim)) { $ImageTrim = 2; } if ($ImageTrimY == 0) { $y = 0; } elseif ($ImageTrimY == 1) { $y = ($newHeight - $destHeight) / 4; } elseif ($ImageTrimY == 2) { $y = ($newHeight - $destHeight) / 2; } elseif ($ImageTrimY == 3) { $y = 3 * ($newHeight - $destHeight) / 2; } elseif ($ImageTrimY == 4) { $y = 5 * ($newHeight - $destHeight) / 2; } if ($ImageTrimX == 0) { $x = 0; } elseif ($ImageTrimX == 1) { $x = ($newWidth - $destWidth) / 4; } elseif ($ImageTrimX == 2) { $x = ($newWidth - $destWidth) / 2; } elseif ($ImageTrimX == 3) { $x = 3 * ($newWidth - $destWidth) / 2; } elseif ($ImageTrimX == 4) { $x = 5 * ($newWidth - $destWidth) / 2; } //print "$ImageTrimY $ImageTrimX $x $y"; //exit; $destImage = imagecreatetruecolor($newWidth, $newHeight); imagecopyresampled($destImage, $srcImage, $x, $y, 0, 0, $destWidth, $destHeight, $srcWidth, $srcHeight); } else { $destImage = imagecreatetruecolor($destWidth, $destHeight); imagecopyresampled($destImage, $srcImage, 0, 0, 0, 0, $destWidth, $destHeight, $srcWidth, $srcHeight); } if ($this->mix) { $destImage = $this->mix($destImage); } $name = $engine_path . "img/small/" . $this->name; if ($imType == "jpeg" || $imType == "pjpeg") { Imagejpeg($destImage, $name); } elseif ($imType == "gif") { Imagegif($destImage, $name); } else { Imagepng($destImage, $name); } $file['tmp_name'] = $name; ImageDestroy($srcImage); ImageDestroy($destImage); return $file; }
function resizeImage($originalImage, $newImage, $newWidth, $newHeight, $bgred = 0xff, $bggreen = 0xff, $bgblue = 0xff, $interlace = 1, $jpegquality = 75) { //abre imagem original switch ($imageType = exif_imagetype($originalImage)) { case IMAGETYPE_GIF: if (imagetypes() & IMG_GIF) { $imOriginal = @imagecreatefromgif($originalImage); } else { die('<P>Formato de imagem não suportado pelo PHP: ' . $imageType . ' ' . image_type_to_mime_type($imageType) . '.</P>'); } break; case IMAGETYPE_JPEG: if (imagetypes() & IMG_JPG) { $imOriginal = @imagecreatefromjpeg($originalImage); } else { die('<P>Formato de imagem não suportado pelo PHP: ' . $imageType . ' ' . image_type_to_mime_type($imageType) . '.</P>'); } break; case IMAGETYPE_PNG: if (imagetypes() & IMG_PNG) { $imOriginal = @imagecreatefrompng($originalImage); } else { die('<P>Formato de imagem não suportado pelo PHP: ' . $imageType . ' ' . image_type_to_mime_type($imageType) . '.</P>'); } break; default: die('<P>Formato de imagem não reconhecido pelo algoritmo: ' . $imageType . ' ' . image_type_to_mime_type($imageType) . '.</P>'); } if (!$imOriginal) { return false; } //cria a imagem nova $imThumb = @imagecreatetruecolor($newWidth, $newHeight); if (!$imThumb) { return false; } imagefilledrectangle($imThumb, 0, 0, $newWidth, $newHeight, imagecolorallocate($imThumb, $bgred, $bggreen, $bgblue)); //cuida do aspect ratio $xOrig = imagesx($imOriginal); $yOrig = imagesy($imOriginal); $xThumb = $newWidth; $yThumb = $newHeight; $ratioOrig = $xOrig / $yOrig; $ratioThumb = $xThumb / $yThumb; if ($ratioOrig > $ratioThumb) { $yThumb = round($xThumb / $ratioOrig); } elseif ($ratioOrig < $ratioThumb) { $xThumb = round($yThumb * $ratioOrig); } $xMargin = round(($newWidth - $xThumb) / 2); $yMargin = round(($newHeight - $yThumb) / 2); //modifica o tamanho imagecopyresampled($imThumb, $imOriginal, $xMargin, $yMargin, 0, 0, $xThumb, $yThumb, $xOrig, $yOrig); ImageDestroy($imOriginal); //entrelaça, se necessário if ($interlace) { imageinterlace($imThumb, $interlace); } //salva a imagem if (!$newImage) { Header('Content-type: ' . image_type_to_mime_type($imageType)); } switch ($imageType) { case IMAGETYPE_GIF: Imagegif($imThumb, $newImage); break; case IMAGETYPE_JPEG: Imagejpeg($imThumb, $newImage, $jpegquality); break; case IMAGETYPE_PNG: if ($newImage) { Imagepng($imThumb, $newImage); } else { Imagepng($imThumb); } break; } ImageDestroy($imThumb); return true; }
<?php // erstmal ein neues leeres Bild erstellen (breite, höhe) $buchbild = imagecreate(140, 180); // ne Hintergrundfarbe erstellen, die später zur Transparenz genutzt wird // (am Besten ne Farbe, die selten in Bildern verwendet wird) $hintergrundfarbe = imagecolorallocate($buchbild, 255, 0, 255); // eine Grafik erstellen von nem vorhandenen Bild (buch2.gif muss da sein) $buchgrafik = ImageCreateFromGIF("buch2.gif"); // diese Grafik in unser Bild kopieren // Imagecopy(zielgrafik, quellgrafik, zielX, zielY, quelleX, quelleY, quB, quH) Imagecopy($buchbild, $buchgrafik, 0, 0, 0, 0, 140, 180); // eine Textfarbe erstellen $textfarbe = imagecolorallocate($buchbild, 0, 0, 0); // einen Text ALS BILD erstellen // ImageString(zielgrafik,schrift,X,Y,text,textfarbe) ImageString($buchbild, 5, 23, 50, "mein Text", $textfarbe); ImageString($buchbild, 2, 23, 65, "noch einer", $textfarbe); ImageString($buchbild, 1, 23, 100, "von: mir", $textfarbe); // nun unser (evtl. vorher schon transparentes) GIF transparent machen // imagecolortransparent(grafik, farbe) imagecolortransparent($buchbild, $hintergrundfarbe); // und abspeichern (Imagegif(bild, zielort, qualität)) Imagegif($buchbild, "erstelltesBild.gif", 100); ?> <img src="erstelltesBild.gif"> </body> </html>
function _output_image($destination_file, $image_type, $image) { //if destination file is empty image will be output to browser // right now $image_type can be JPEG or PNG global $ERR, $create_text; $destination_file = trim($destination_file); $res = false; //Create Text $white = ImageColorAllocate($image, 255, 255, 255); $black = ImageColorAllocate($image, 0, 0, 0); $red = ImageColorAllocate($image, 255, 0, 0); $blue = ImageColorAllocate($image, 0, 0, 255); $lightblue = ImageColorAllocate($image, 0, 128, 255); $green = ImageColorAllocate($image, 0, 255, 0); $violet = ImageColorAllocate($image, 225, 128, 225); $orange = ImageColorAllocate($image, 255, 128, 0); $gray = ImageColorAllocate($image, 192, 192, 192); $pink = ImageColorAllocate($image, 255, 0, 128); $brown = ImageColorAllocate($image, 128, 0, 0); $darkgreen = ImageColorAllocate($image, 0, 128, 0); ImageString($image, 3, 2, 1, $create_text, $white); if ($image) { switch ($image_type) { case 'JPEG': case 'JPG': $res = ImageJpeg($image, $destination_file, $this->jpeg_quality); break; case 'PNG': $res = Imagepng($image, $destination_file); break; case 'GIF': $res = Imagegif($image, $destination_file); break; default: $this->error($ERR["UNKNOWN_OUTPUT_FORMAT"] . " {$image_type}"); break; } } else { $this->error($ERR["NO_IMAGE_FOR_OUTPUT"]); } if (!$res) { $this->error($ERR["UNABLE_TO_OUTPUT"] . " {$destination_file}"); } return $res; }
function overlay($backpic, $overpic, $x, $y, $w, $h) { $backsize = getimagesize($backpic); $oversize = getimagesize($overpic); if ($backsize[2] == 1) { $dstimg = ImageCreateFromGIF($backpic); } elseif ($backsize[2] == 2) { $dstimg = ImageCreateFromJPEG($backpic); } elseif ($backsize[2] == 3) { $dstimg = ImageCreateFromPNG($backpic); } if ($oversize[2] == 1) { $srcimg = ImageCreateFromGIF($overpic); } elseif ($oversize[2] == 2) { $srcimg = ImageCreateFromJPEG($overpic); } elseif ($oversize[2] == 3) { $srcimg = ImageCreateFromPNG($overpic); } Imagecopymerge($dstimg, $srcimg, $x, $y, 0, 0, $w, $h, 100); if ($backsize[2] == 1) { Imagegif($dstimg, $backpic, 100); } elseif ($backsize[2] == 2) { Imagejpeg($dstimg, $backpic, 100); } elseif ($backsize[2] == 3) { Imagepng($dstimg, $backpic, 0); } @ImageDestroy($dstimg); @ImageDestroy($srcimg); }
function gdfillcolor($srcFile, $dstFile, $dstW, $dstH) { @ImageAlphaBlending($srcFile, true); $picdata = GetImageSize($srcFile); switch ($picdata[2]) { case 1: $im = @ImageCreateFromGIF($srcFile); break; case 2: $im = @imageCreateFromJpeg($srcFile); break; case 3: $im = @ImageCreateFromPNG($srcFile); break; case 6: $im = @ImageCreateFromWbmp($srcFile); break; } $srcW = ImageSX($im); $srcH = ImageSY($im); $dstX = 0; $dstY = 0; if ($srcW * $dstH > $srcH * $dstW) { $fdstH = round($srcH * $dstW / $srcW); $dstY = floor(($dstH - $fdstH) / 2); $fdstW = $dstW; } else { $fdstW = round($srcW * $dstH / $srcH); $dstX = floor(($dstW - $fdstW) / 2); $fdstH = $dstH; } $dstX = $dstX < 0 ? 0 : $dstX; $dstY = $dstX < 0 ? 0 : $dstY; $dstX = $dstX > $dstW / 2 ? floor($dstW / 2) : $dstX; $dstY = $dstY > $dstH / 2 ? floor($dstH / s) : $dstY; $ni = imagecreatetruecolor($dstW, $dstH); $black = ImageColorAllocate($ni, 255, 255, 255); $white = ImageColorAllocate($ni, 255, 255, 255); imagefilledrectangle($ni, 0, 0, $dstW, $dstH, $black); // 填充剩余背景色 if (function_exists("imagecopyresampled")) { // 改变图片大小 imagecopyresampled($ni, $im, $dstX, $dstY, 0, 0, $fdstW, $fdstH, $srcW, $srcH); } else { imagecopyresized($ni, $im, $dstX, $dstY, 0, 0, $fdstW, $fdstH, $srcW, $srcH); } if (function_exists("Imagegif")) { Imagegif($ni, $dstFile); // 输出到文件 } else { ImageJpeg($ni, $dstFile); } imagedestroy($im); // 清理内存 imagedestroy($ni); }
function uploadimg($filename, $width, $get_height, $path) { if (trim($_FILES["image"]["tmp_name"]) != "") { $tmp_images = $_FILES["image"]["tmp_name"]; // type select if ($_FILES['image']['type'] == 'image/jpeg' or $_FILES['image']['type'] == 'image/jpg' or $_FILES['image']['type'] == 'image/pjpeg') { $images = $filename . ".jpg"; //upload source image $size = getimagesize($tmp_images); //check radio widht and height $height = round($width * $size[1] / $size[0]); if ($height > $get_height) { $width = round($get_height * $size[0] / $size[1]); $height = $get_height; } $images_orig = ImageCreateFromJPEG($tmp_images); $photoX = ImagesX($images_orig); $photoY = ImagesY($images_orig); $images_fin = ImageCreateTrueColor($width, $height); ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width + 1, $height + 1, $photoX, $photoY); ImageJPEG($images_fin, $path . $images); ImageDestroy($images_orig); ImageDestroy($images_fin); return $filename . ".jpg"; } elseif ($_FILES['image']['type'] == 'image/x-png' or $_FILES['image']['type'] == 'image/png') { $images = $filename . ".png"; $size = getimagesize($tmp_images); //check radio widht and height $height = round($width * $size[1] / $size[0]); if ($height > $get_height) { $width = round($get_height * $size[0] / $size[1]); $height = $get_height; } $images_orig = ImageCreateFromPNG($tmp_images); $photoX = ImagesX($images_orig); $photoY = ImagesY($images_orig); $images_fin = ImageCreateTrueColor($width, $height); ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width + 1, $height + 1, $photoX, $photoY); Imagepng($images_fin, $path . $images); ImageDestroy($images_orig); ImageDestroy($images_fin); return $filename . ".png"; } elseif ($_FILES['image']['type'] == 'image/gif') { $images = $filename . ".gif"; $size = getimagesize($tmp_images); //check radio widht and height $height = round($width * $size[1] / $size[0]); if ($height > $get_height) { $width = round($get_height * $size[0] / $size[1]); $height = $get_height; } $images_orig = ImageCreateFromgif($tmp_images); $photoX = ImagesX($images_orig); $photoY = ImagesY($images_orig); $images_fin = ImageCreateTrueColor($width, $height); ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width + 1, $height + 1, $photoX, $photoY); Imagegif($images_fin, $path . $images); ImageDestroy($images_orig); ImageDestroy($images_fin); return $filename . ".gif"; } else { return FALSE; } } }
break; case 'image/png': $src = imagecreatefrompng($local); break; case 'image/gif': $src = imagecreatefromgif($local); break; default: break; } $sx = imagesx($src); //获取原图的宽 $sy = imagesy($src); //获取原图的高 $new_image = imagecreatetruecolor($width, $height); //用刚才给定的长和宽建立一张白的缩略图。 imagecopyresized($new_image, $src, 0, 0, 0, 0, $width, $height, $sx, $sy); //把原图压到这张白图里头 if (Imagejpeg($new_image, $localtion) || Imagegif($new_image, $localtion) || Imagepng($new_image, $localtion)) { //转移缩略图 echo "制作完成"; echo "<img src='{$localtion}' mce_src='{$localtion}'>"; //将缩略图显示 } } else { echo " <script> alert('验证码输入错误');history.back()</script>"; } } } else { echo "不存在"; }
function imageResize() { global $engine_path, $ImageTrimX, $ImageTrimY; $source = $this->image; $newWidth = $this->newWidth; $newHeight = $this->newHeight; $imType = $this->type; if (!$imType || $imType == "jpeg" || $imType == "pjpeg") { $srcImage = ImageCreateFromJPEG($source); } elseif ($imType == "gif") { $srcImage = LoadGif($source); } else { $srcImage = ImageCreateFromPNG($source); } $srcWidth = ImageSX($srcImage); $srcHeight = ImageSY($srcImage); $ratioWidth = $srcWidth / $newWidth; $ratioHeight = $srcHeight / $newHeight; if ($ratioWidth < 1 && $this->fix == "width" || $ratioHeight < 1 && $this->fix == "height") { $destWidth = $srcWidth; $destHeight = $srcHeight; $mode = 1; } elseif ($ratioWidth > $ratioHeight && $newHeight != 120 || $ratioWidth < $ratioHeight && $this->fix != "width" || $this->fix == "height") { $destWidth = $srcWidth / $ratioHeight; $destHeight = $newHeight; $mode = 2; } else { $destWidth = $newWidth; $destHeight = $srcHeight / $ratioWidth; $mode = 3; } //print "$ratioWidth > $ratioHeight) || ( $ratioWidth < $ratioHeight && $this->fix ".$mode; //exit; if ($newWidth != $srcWidth && $newHeight != $srcHeight && $newWidth != 160 && $mode != 1 && $newHeight != 120) { if (!strlen($ImageTrim)) { $ImageTrim = 2; } if ($newy) { $y = $newy; } elseif ($ImageTrimY == 0) { $y = 0; } elseif ($ImageTrimY == 1) { $y = ($newHeight - $destHeight) / 4; } elseif ($ImageTrimY == 2) { $y = ($newHeight - $destHeight) / 2; } elseif ($ImageTrimY == 3) { $y = 3 * ($newHeight - $destHeight) / 2; } elseif ($ImageTrimY == 4) { $y = 5 * ($newHeight - $destHeight) / 2; } if ($newx) { $x = $newx; } elseif ($ImageTrimX == 0) { $x = 0; } elseif ($ImageTrimX == 1) { $x = ($newWidth - $destWidth) / 4; } elseif ($ImageTrimX == 2) { $x = ($newWidth - $destWidth) / 2; } elseif ($ImageTrimX == 3) { $x = 3 * ($newWidth - $destWidth) / 2; } elseif ($ImageTrimX == 4) { $x = 5 * ($newWidth - $destWidth) / 2; } $destImage = imagecreatetruecolor($newWidth, $newHeight); imagecopyresampled($destImage, $srcImage, $x, $y, 0, 0, $destWidth, $destHeight, $srcWidth, $srcHeight); } else { $destImage = imagecreatetruecolor($destWidth, $destHeight); imagecopyresampled($destImage, $srcImage, 0, 0, 0, 0, $destWidth, $destHeight, $srcWidth, $srcHeight); } $this->newWidth = $destWidth; $this->newHeight = $destHeight; if ($this->mix) { $destImage = $this->mix($destImage); } $name = $engine_path . "tmp/" . $this->name; if (!file_exists($engine_path . "tmp/")) { mkdir_r($engine_path . "tmp/"); } if ($imType == "jpeg" || $imType == "pjpeg") { Imagejpeg($destImage, $name); } elseif ($imType == "gif") { Imagegif($destImage, $name); } else { Imagepng($destImage, $name); } $file['tmp_name'] = $name; ImageDestroy($srcImage); ImageDestroy($destImage); return $file; }
function show() { Header("Content-type: image/gif"); /* * 初始化 */ $border = 0; //是否要边框 1要:0不要 $how = $this->num; //验证码位数 $w = $this->width; //图片宽度 $h = $this->height; //图片高度 $fontsize = 5; //字体大小 $alpha = "abcdefghijkmnpqrstuvwxyz"; //验证码内容1:字母 $number = "23456789"; //验证码内容2:数字 $randcode = ""; //验证码字符串初始化 srand((double) microtime() * 1000000); //初始化随机数种子 $im = ImageCreate($w, $h); //创建验证图片 /* * 绘制基本框架 */ $bgcolor = ImageColorAllocate($im, 255, 255, 255); //设置背景颜色 ImageFill($im, 0, 0, $bgcolor); //填充背景色 if ($border) { $black = ImageColorAllocate($im, 0, 0, 0); //设置边框颜色 ImageRectangle($im, 0, 0, $w - 1, $h - 1, $black); //绘制边框 } /* * 逐位产生随机字符 */ for ($i = 0; $i < $how; $i++) { $alpha_or_number = mt_rand(0, 1); //字母还是数字 $str = $alpha_or_number ? $alpha : $number; $which = mt_rand(0, strlen($str) - 1); //取哪个字符 $code = substr($str, $which, 1); //取字符 $j = !$i ? 4 : $j + 15; //绘字符位置 $color3 = ImageColorAllocate($im, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100)); //字符随即颜色 ImageChar($im, $fontsize, $j, 3, $code, $color3); //绘字符 $randcode .= $code; //逐位加入验证码字符串 } /* * 添加干扰 * 目的是为了不让机器轻易破解 */ for ($i = 0; $i < 5; $i++) { $color1 = ImageColorAllocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); //干扰线颜色 ImageArc($im, mt_rand(-5, $w), mt_rand(-5, $h), mt_rand(20, 300), mt_rand(20, 200), 55, 44, $color1); //干扰线 } for ($i = 0; $i < $how * 15; $i++) { $color2 = ImageColorAllocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); //干扰点颜色 ImageSetPixel($im, mt_rand(0, $w), mt_rand(0, $h), $color2); //干扰点 } //把验证码字符串写入session //$this->session->set_userdata(array($this->name=>$randcode)); //???????????????/ $_SESSION[$this->name] = $randcode; /*绘图结束*/ Imagegif($im); ImageDestroy($im); /*绘图结束*/ }
function imageMix($mix) { global $engine_path, $ImageTrimX, $ImageTrimY; $source = $this->image; $imType = $this->type; if (!$imType || $imType == "jpeg" || $imType == "pjpeg") { $srcImage = ImageCreateFromJPEG($source); } elseif ($imType == "gif") { $srcImage = LoadGif($source); } else { $srcImage = ImageCreateFromPNG($source); } if ($mix) { $srcImage = $this->mix($srcImage, $mix); } $name = $engine_path . "tmp/" . "mix" . $this->name; if (!file_exists($engine_path . "tmp/")) { mkdir_r($engine_path . "tmp/"); } if ($imType == "jpeg" || $imType == "pjpeg") { Imagejpeg($srcImage, $name); } elseif ($imType == "gif") { Imagegif($srcImage, $name); } else { Imagepng($srcImage, $name); } $file['tmp_name'] = $name; ImageDestroy($srcImage); return $file; }
function resizeimage($imgp, $imgw, $imgh) { $imagepath = base64_decode($imgp); #echo '.... ' . base64_decode( $imgp ); #flush();die(' stop!'); # Thanks to Michał Albrecht! $suffix = strtolower(substr($imagepath, -4)); $imgsize = @getimagesize($imagepath); # http://be.php.net/getimagesize $head = "Content-type: {$imgsize['mime']}"; if ($suffix == ".gif") { #$head = "Content-type: image/gif"; $img = imagecreatefromgif($imagepath); if (!$img) { brokenimage("Error loading GIF"); } } else { if ($suffix == ".jpg" or $suffix == "jpeg") { #$head = "Content-type: image/jpeg"; $img = imagecreatefromjpeg($imagepath); if (!$img) { brokenimage("Error loading JPG"); } } else { if ($suffix == ".png") { #$head = "Content-type: image/png"; $img = imagecreatefrompng($imagepath); if (!$img) { brokenimage("Error loading PNG"); } } } } # Resize the image $src_h = ImageSY($img); $src_w = ImageSX($img); $dst_img = imagecreatetruecolor($imgw, $imgh); imagecopyresampled($dst_img, $img, 0, 0, 0, 0, $imgw, $imgh, $src_w, $src_h); $img = $dst_img; imageinterlace($img, 1); imagecolortransparent($img); ob_start(); if ($suffix == ".gif") { Imagegif($img); } else { if ($suffix == ".jpg" or $suffix == ".jpeg") { Imagejpeg($img, '', 80); } else { if ($suffix == ".png") { Imagepng($img); } } } $result = ob_get_clean(); #ImageDestroy($img); $result = serialize(array($head, base64_encode($result))); return $result; }
function showImage($type = 'png', $quality = 100) { if ($this->_imgFinal == null) { $this->_debug('showImage', 'There is no processed image to show.'); return false; } if ($type == 'png') { echo @header("Content-Type: image/png"); echo @ImagePNG($this->_imgFinal); return true; } else { if ($type == 'jpg' || $type == 'jpeg') { echo @header("Content-Type: image/jpeg"); echo @ImageJPEG($this->_imgFinal, '', $quality); return true; } else { if ($type == 'gif') { echo @header("Content-Type: image/gif"); echo @Imagegif($this->_imgFinal); return true; } else { $this->_debug('showImage', "Could not show the output file as a {$type}."); return false; } } } }
public function show($callback = NULL) { Header('Content-type: image/gif'); /* * 初始化 */ $border = 0; //是否要边框 1要:0不要 $how = $this->num; //验证码位数 $w = $this->width; //图片宽度 $h = $this->height; //图片高度 $fontsize = 5; //字体大小 $alpha = 'abcdefghijkmnopqrstuvwxyz'; //验证码内容1:字母 $number = '023456789'; //验证码内容2:数字 $randcode = ''; //验证码字符串初始化 srand((double) microtime() * 1000000); //初始化随机数种子 $im = ImageCreate($w, $h); //创建验证图片 /* * 绘制基本框架 */ $bgcolor = ImageColorAllocate($im, 255, 255, 255); //设置背景颜色 ImageFill($im, 0, 0, $bgcolor); //填充背景色 if ($border) { $black = ImageColorAllocate($im, 0, 0, 0); //设置边框颜色 ImageRectangle($im, 0, 0, $w - 1, $h - 1, $black); //绘制边框 } /* * 逐位产生随机字符 */ for ($i = 0; $i < $how; $i++) { $alpha_or_number = mt_rand(0, 1); //字母还是数字 $str = $alpha_or_number ? $alpha : $number; $which = mt_rand(0, strlen($str) - 1); //取哪个字符 $code = substr($str, $which, 1); //取字符 $j = !$i ? 4 : $j + 15; //绘字符位置 $color3 = ImageColorAllocate($im, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100)); //字符随即颜色 ImageChar($im, $fontsize, $j, 3, $code, $color3); //绘字符 $randcode .= $code; //逐位加入验证码字符串 } /* * 添加干扰 */ for ($i = 0; $i < 5; $i++) { $color1 = ImageColorAllocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); //干扰线颜色 ImageArc($im, mt_rand(-5, $w), mt_rand(-5, $h), mt_rand(20, 300), mt_rand(20, 200), 55, 44, $color1); //干扰线 } for ($i = 0; $i < $how * 15; $i++) { $color2 = ImageColorAllocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); //干扰点颜色 ImageSetPixel($im, mt_rand(0, $w), mt_rand(0, $h), $color2); //干扰点 } //把验证码字符串写入session //$_SESSION[$this->name]=$randcode; // if (isset($this->output_conf)) { // if (is_array($this->output_conf)) { // $this->output_conf['verify_code'] = $randcode; // } else { // $this->output_conf->verify_code = $randcode; // } // } // $this->CI->session->set_userdata('verify_code', $randcode); if (isset($callback)) { $callback($randcode); } /*绘图结束*/ Imagegif($im); ImageDestroy($im); /*绘图结束*/ return $randcode; }
public function get_image($session_key = 'captcha') { header("Content-type:image/png"); if (!isset($_SESSION)) { session_start(); } /* * 初始化 */ $border = 0; //是否要边框 1要:0不要 $how = $this->code_num; //验证码位数 $w = $this->width; //图片宽度 $h = $this->height; //图片高度 $fontsize = 20; //字体大小 $alpha = "ABCDEFGHJKLMNPQRSTUVWXYZ"; //验证码内容1:字母 $number = "23456789"; //验证码内容2:数字 $randcode = ""; //验证码字符串初始化 $font_url = 'fonts/simhei.ttf'; srand((double) microtime() * 1000000); //初始化随机数种子 $im = ImageCreate($w, $h); //创建验证图片 /* * 绘制基本框架 */ $bgcolor = ImageColorAllocate($im, 255, 255, 255); //设置背景颜色 ImageFill($im, 0, 0, $bgcolor); //填充背景色 if ($border) { $black = ImageColorAllocate($im, 0, 0, 0); //设置边框颜色 ImageRectangle($im, 0, 0, $w - 1, $h - 1, $black); //绘制边框 } /* * 逐位产生随机字符 */ for ($i = 0; $i < $how; $i++) { $alpha_or_number = mt_rand(0, 1); //字母还是数字 $str = $alpha_or_number ? $alpha : $number; $which = mt_rand(0, strlen($str) - 1); //取哪个字符 $code = substr($str, $which, 1); //取字符 $j = !$i ? 15 : $j + 12; //绘字符位置 $color3 = ImageColorAllocate($im, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100)); //字符随即颜色 #ImageChar($im, $fontsize, $j, 6, $code, $color3); //绘字符 //旋转角度随机数 $angel_rand_1 = mt_rand(-15, -10); $angel_rand_2 = mt_rand(10, 15); $angel_rand_change = mt_rand(0, 1); $angel_rand = $angel_rand_change == 1 ? $angel_rand_1 : $angel_rand_2; ImageTTFText($im, $fontsize, $angel_rand, $j, 25, $color3, $font_url, $code); $randcode .= $code; //逐位加入验证码字符串 } /* * 添加干扰 */ for ($i = 0; $i < 5; $i++) { $color1 = ImageColorAllocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); //干扰线颜色 ImageArc($im, mt_rand(-5, $w), mt_rand(-5, $h), mt_rand(20, 300), mt_rand(20, 200), 55, 44, $color1); //干扰线 } for ($i = 0; $i < $how * 40; $i++) { $color2 = ImageColorAllocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); //干扰点颜色 ImageSetPixel($im, mt_rand(0, $w), mt_rand(0, $h), $color2); //干扰点 } //把验证码字符串写入session $_SESSION[$session_key] = $randcode; /*绘图结束*/ Imagegif($im); ImageDestroy($im); /*绘图结束*/ }
function create_my_captcha() { Header("Content-type: image/gif"); /* * 初始化 */ $border = 0; //是否要边框 1要:0不要 $how = 4; //验证码位数 $w = $how * 15; //图片宽度 $h = 20; //图片高度 $fontsize = 16; //字体大小 $alpha = "abcdefghijkmnpqrstuvwxyzABCDEFGHIGKLMNPQRSTUVWXYZ"; //验证码内容1:字母 $number = "23456789"; //验证码内容2:数字 $randcode = ""; //验证码字符串初始化 srand((double) microtime() * 1000000); //初始化随机数种子 $im = ImageCreate($w, $h); //创建验证图片 /* * 绘制基本框架 */ $bgcolor = ImageColorAllocate($im, 255, 255, 255); //设置背景颜色 ImageFill($im, 0, 0, $bgcolor); //填充背景色 if ($border) { $black = ImageColorAllocate($im, 0, 0, 0); //设置边框颜色 ImageRectangle($im, 0, 0, $w - 1, $h - 1, $black); //绘制边框 } /* * 逐位产生随机字符 */ for ($i = 0; $i < $how; $i++) { $alpha_or_number = mt_rand(0, 1); //字母还是数字 $str = $alpha_or_number ? $alpha : $number; $which = mt_rand(0, strlen($str) - 1); //取哪个字符 $code = substr($str, $which, 1); //取字符 $j = !$i ? 4 : $j + 15; //绘字符位置 $color3 = ImageColorAllocate($im, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100)); //字符随即颜色 ImageChar($im, $fontsize, $j, 3, $code, $color3); //绘字符 $randcode .= $code; //逐位加入验证码字符串 } /* * 添加干扰 */ for ($i = 0; $i < 2; $i++) { $color1 = ImageColorAllocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); //干扰线颜色 ImageArc($im, mt_rand(-5, $w), mt_rand(-5, $h), mt_rand(20, 300), mt_rand(20, 200), 55, 44, $color1); //干扰线 } for ($i = 0; $i < $how * 7; $i++) { $color2 = ImageColorAllocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); //干扰点颜色 ImageSetPixel($im, mt_rand(0, $w), mt_rand(0, $h), $color2); //干扰点 } //把验证码字符串写入session $_SESSION['login_check_number'] = strtoupper($randcode); /*绘图结束*/ Imagegif($im); ImageDestroy($im); /*绘图结束*/ }
function setThumbnail($file, $upload_dir = ".", $thumbnail_upload_dir = "./thumb", $filename = "", $thumbnail_size = 250, $thumbnail_quality = 76, $limit_size = 2, $type = 1, $check = 0, $image_type = 0, $image_width = 800, $image_height = 600, $limit_length = 30) { if ($filename == "") { $filename = $file[name]; } $img_size = $this->setFileUpload($file, $upload_dir, $filename, $limit_size, $type, $check, $image_type, $image_width, $image_height = 600, $limit_length = 30); //앨범에서의 썸네일 이미지를 위해 JPG GD를 사용하여 썸네일 이미지를 생성한다. if ($img_size[0] > $img_size[1]) { //가로방향 이미지 if ($img_size[0] > $thumbnail_size) { $img_width = $thumbnail_size; $img_height = $img_size[1] * $thumbnail_size / $img_size[0]; } else { $img_width = $img_size[0]; $img_height = $img_size[1]; } } else { //세로방향 이미지 if ($img_size[1] > $thumbnail_size) { $img_width = $img_size[0] * $thumbnail_size / $img_size[1]; $img_height = $thumbnail_size; } else { $img_width = $img_size[0]; $img_height = $img_size[1]; } } if ($file[type] == "image/pjpeg" && (eregi("\\.jpg\$", $file[name]) || eregi("\\.jpeg\$", $file[name])) && $this->varGDPermit[jpg] == 'Y') { $dst_img = ImageCreate($img_width, $img_height); // 위에서 계산한대로 빈 이미지 파일을 만듭니다. $src_img = ImageCreateFromjpeg("{$upload_dir}/{$filename}"); // 이미지파일을 읽어들입니다. ImageCopyResized($dst_img, $src_img, 0, 0, 0, 0, $img_width, $img_height, ImageSX($src_img), ImageSY($src_img)); //빈 이미지에다 원본이미지를 줄여서 그립니다. $creat_file_name = "{$thumbnail_upload_dir}/{$filename}"; Imagejpeg($dst_img, $creat_file_name, $thumbnail_quality); // thumb 디렉토리에 원본과 같은 이름의 jpeg으로 저장합니다. 76이라는 숫자는 퀄리티인데요, 높을수록 용량크고 질이 좋아집니다. } else { if ($file[type] == "image/x-png" && eregi("\\.png\$", $file[name]) && $this->varGDPermit[png] == 'Y') { $dst_img = ImageCreate($img_width, $img_height); // 위에서 계산한대로 빈 이미지 파일을 만듭니다. $src_img = ImageCreateFrompng("{$upload_dir}/{$filename}"); // 이미지파일을 읽어들입니다. ImageCopyResized($dst_img, $src_img, 0, 0, 0, 0, $img_width, $img_height, ImageSX($src_img), ImageSY($src_img)); //빈 이미지에다 원본이미지를 줄여서 그립니다. $creat_file_name = "{$thumbnail_upload_dir}/{$filename}"; Imagepng($dst_img, $creat_file_name, $thumbnail_quality); // thumb 디렉토리에 원본과 같은 이름의 jpeg으로 저장합니다. 76이라는 숫자는 퀄리티인데요, 높을수록 용량크고 질이 좋아집니다. } else { if ($file[type] == "image/gif" && eregi("\\.gif\$", $file[name]) && $this->varGDPermit[gif] == 'Y') { $dst_img = ImageCreate($img_width, $img_height); // 위에서 계산한대로 빈 이미지 파일을 만듭니다. $src_img = ImageCreateFromgif("{$upload_dir}/{$filename}"); // 이미지파일을 읽어들입니다. ImageCopyResized($dst_img, $src_img, 0, 0, 0, 0, $img_width, $img_height, ImageSX($src_img), ImageSY($src_img)); //빈 이미지에다 원본이미지를 줄여서 그립니다. $creat_file_name = "{$thumbnail_upload_dir}/{$filename}"; Imagegif($dst_img, $creat_file_name, $thumbnail_quality); // thumb 디렉토리에 원본과 같은 이름의 jpeg으로 저장합니다. 76이라는 숫자는 퀄리티인데요, 높을수록 용량크고 질이 좋아집니다. } else { $this->errmsg("{$file['type']}은 GD를 지원하지 않습니다!!"); } } } ImageDestroy($dst_img); //로드한 메모리를 비워줍니다. gd는 꼭 이걸 해주어야 합니다. ImageDestroy($src_img); }
public function func_resize_save($dst_file, $src_file, $save_path, $max_x, $max_y) { $img_size = getimagesize($src_file); $img_replace = basename($src_file); switch ($img_size[2]) { //gif case 1: $src_img = ImageCreateFromgif($src_file); $dst_img = ImageCreateTrueColor($max_x, $max_y); ImageCopyResampled($dst_img, $src_img, 0, 0, 0, 0, $max_x, $max_y, $img_size[0], $img_size[1]); Imagegif($dst_img, $save_path . $img_replace, 100); break; //jpeg or jpg //jpeg or jpg case 2: $src_img = ImageCreateFromjpeg($src_file); $dst_img = ImageCreateTrueColor($max_x, $max_y); ImageCopyResampled($dst_img, $src_img, 0, 0, 0, 0, $max_x, $max_y, $img_size[0], $img_size[1]); Imagejpeg($dst_img, $save_path . $img_replace, 100); break; //png //png case 3: $src_img = ImageCreateFrompng($src_file); $dst_img = ImageCreateTrueColor($max_x, $max_y); ImageCopyResampled($dst_img, $src_img, 0, 0, 0, 0, $max_x, $max_y, $img_size[0], $img_size[1]); Imagepng($dst_img, $save_path . $img_replace, 9); break; } ImageDestroy($dst_img); ImageDestroy($src_img); }
public function createBarCode($image_type = 'jpg', $file_name = null) { $this->parseCode(); //新建一个调色板 $this->image = ImageCreate($this->width + 2 * $this->quiet_zone, $this->heith + $this->font_height); $this->bgcolor = imagecolorallocate($this->image, $this->bgcolor >> 16, $this->bgcolor >> 8 & 0xff, $this->bgcolor & 0xff); $this->color = imagecolorallocate($this->image, $this->color >> 16, $this->color >> 8 & 0xff, $this->color & 0xff); ImageFilledRectangle($this->image, 0, 0, $this->width + 2 * $this->quiet_zone, $this->heith + $this->font_height, $this->bgcolor); $sx = $this->quiet_zone; //$sy = $this->font_height -1; $sy = 0; $fw = 10; //編號為2或3的字體的寬度為10,為4或5的字體寬度為11 if ($this->font_type > 3) { $sy++; $fw = 11; } $ex = 0; //$ey = $this->heith + $this->font_height - 2; $ey = $this->heith; for ($i = 0; $i < strlen($this->bin_code); $i++) { $ex = $sx + $this->unit_width * (int) $this->bin_code[$i] - 1; if ($i % 2 == 0) { ImageFilledRectangle($this->image, $sx, $sy, $ex, $ey, $this->color); } $sx = $ex + 1; } $t_num = strlen($this->text); $t_x = $this->width / $t_num; $t_sx = ($t_x - $fw) / 2; //目的为了使文字居中平均分布 for ($i = 0; $i < $t_num; $i++) { imagechar($this->image, $this->font_type, 6 * $this->unit_width + $t_sx + $i * $t_x, $this->heith, $this->text[$i], $this->color); } if (!$file_name) { header("Content-Type: image/" . $image_type); } switch ($image_type) { case 'jpg': case 'jpeg': return Imagejpeg($this->image, $file_name); break; case 'png': Imagepng($this->image, $file_name); break; case 'gif': break; Imagegif($this->image, $file_name); default: Imagepng($this->image, $file_name); break; } }
include 'includes/pclzip.lib.php'; $archive = new PclZip($newpath . $row[id] . ".zip"); if ($archive->extract($font_contents_path . $row[id] . "/", $row[id]) == 0) { $font = ""; } } $bbox = imagettfbbox($fontsize, 0, $font, $imagetext); while ($bbox[1] + $bbox[7] > $imageheight) { $fontsize = $fontsize - 2; $bbox = imagettfbbox($fontsize, 0, $font, $imagetext); } $xc = $bbox[0] + $bbox[2]; $yc = $bbox[1] + $bbox[7]; $xpos = 17; $ypos = 70; $result = imagettftext($im, $fontsize, 0, $xpos, $ypos, $color, $font, $imagetext); $trans_color = imagecolorallocate($im, 255, 255, 255); imagecolortransparent($im, $trans_color); $filename = $row[id] . ".ttf"; $filename = str_replace(".ttf", ".gif", $filename); $newname = $previewspath . $filename; Imagegif($im, $newname); imagedestroy($im); } } header("Content-type: image/gif"); $filename = $previewspath . $thumbid . ".gif"; $handle = fopen($filename, "r"); $contents = fread($handle, filesize($filename)); echo $contents; exit;