/** * @throws Exception * 生成验证码 , 存放到token相对应的session里面 */ public function createCaptcha() { $token = $_COOKIE["token"]; if (empty($token)) { throw new Exception("无法生产验证码,请稍后在试"); } $string = "abcdefghijklmnopqrstuvwxyz0123456789"; $str = ""; for ($i = 0; $i < 4; $i++) { $pos = rand(0, 35); $str .= $string[$pos]; } $this->load->library('session'); $flag = $this->session->set_userdata($token . "_captcha", $str); $img_handle = Imagecreate(80, 20); //图片大小80X20 $back_color = ImageColorAllocate($img_handle, 255, 255, 255); //背景颜色(白色) $txt_color = ImageColorAllocate($img_handle, 0, 0, 0); //文本颜色(黑色) //加入干扰线 for ($i = 0; $i < 3; $i++) { $line = ImageColorAllocate($img_handle, rand(0, 255), rand(0, 255), rand(0, 255)); Imageline($img_handle, rand(0, 15), rand(0, 15), rand(100, 150), rand(10, 50), $line); } //加入干扰象素 for ($i = 0; $i < 200; $i++) { $randcolor = ImageColorallocate($img_handle, rand(0, 255), rand(0, 255), rand(0, 255)); Imagesetpixel($img_handle, rand() % 100, rand() % 50, $randcolor); } Imagefill($img_handle, 0, 0, $back_color); //填充图片背景色 ImageString($img_handle, 28, 20, 0, $str, $txt_color); //水平填充一行字符串 ob_clean(); // ob_clean()清空输出缓存区 header("Content-type: image/png"); //生成验证码图片 Imagepng($img_handle); //显示图片 }
function make_thumb($image_path, $image_name, $height, $width) { // echo $image_path."-".$image_name."-".$height."-".$width; /* read the source image */ $ext = pathinfo($image_name, PATHINFO_EXTENSION); if ($ext == 'jpg' || $ext == 'jpeg') { $source_image = imagecreatefromjpeg($image_name); } elseif ($ext == 'png') { $source_image = imagecreatefrompng($image_name); } elseif ($ext == 'gif') { $source_image = imagecreatefromgif($image_name); } elseif ($ext == 'bmp') { $source_image = imagecreatefrombmp($image_name); } $actual_width = imagesx($source_image); $actual_height = imagesy($source_image); /* create a new, "virtual" image */ $virtual_image = imagecreatetruecolor($width, $height); Imagefill($virtual_image, 0, 0, imagecolorallocate($virtual_image, 255, 255, 255)); /* copy source image at a resized size */ imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $width, $height, $actual_width, $actual_height); /* create the physical thumbnail image to its destination */ imagejpeg($virtual_image, $image_path); }
function createthumb($orig_name, $name, $newname, $new_w, $new_h, $border = false, $transparency = true, $base64 = false) { if (file_exists($newname)) { @unlink($newname); } if (!file_exists($name)) { return false; } $arr = explode(".", $orig_name); $ext = $arr[count($arr) - 1]; if ($ext == "jpeg" || $ext == "jpg") { $img = @imagecreatefromjpeg($name); } elseif ($ext == "png") { $img = @imagecreatefrompng($name); } elseif ($ext == "gif") { $img = @imagecreatefromgif($name); } if (!$img) { return false; } $old_x = imageSX($img); $old_y = imageSY($img); if ($old_x < $new_w && $old_y < $new_h) { $thumb_w = $old_x; $thumb_h = $old_y; } elseif ($old_x > $old_y) { $thumb_w = $new_w; $thumb_h = floor($old_y * $new_w / $old_x); } else { $thumb_w = floor($old_x * $new_h / $old_y); $thumb_h = $new_h; } $thumb_w = $thumb_w < 1 ? 1 : $thumb_w; $thumb_h = $thumb_h < 1 ? 1 : $thumb_h; $new_img = ImageCreateTrueColor($thumb_w, $thumb_h); if ($transparency) { if ($ext == "png") { imagealphablending($new_img, false); $colorTransparent = imagecolorallocatealpha($new_img, 0, 0, 0, 127); imagefill($new_img, 0, 0, $colorTransparent); imagesavealpha($new_img, true); } elseif ($ext == "gif") { $trnprt_indx = imagecolortransparent($img); if ($trnprt_indx >= 0) { //its transparent $trnprt_color = imagecolorsforindex($img, $trnprt_indx); $trnprt_indx = imagecolorallocate($new_img, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']); imagefill($new_img, 0, 0, $trnprt_indx); imagecolortransparent($new_img, $trnprt_indx); } } } else { Imagefill($new_img, 0, 0, imagecolorallocate($new_img, 255, 255, 255)); } imagecopyresampled($new_img, $img, 0, 0, 0, 0, $thumb_w, $thumb_h, $old_x, $old_y); if ($border) { $black = imagecolorallocate($new_img, 0, 0, 0); imagerectangle($new_img, 0, 0, $thumb_w, $thumb_h, $black); } if ($base64) { ob_start(); imagepng($new_img); $img = ob_get_contents(); ob_end_clean(); $return = base64_encode($img); } else { if ($ext == "jpeg" || $ext == "jpg") { imagejpeg($new_img, $newname); $return = true; } elseif ($ext == "png") { imagepng($new_img, $newname); $return = true; } elseif ($ext == "gif") { imagegif($new_img, $newname); $return = true; } } imagedestroy($new_img); imagedestroy($img); return $return; }
private function _fvMakeImage($img) { $Bot_path = getcwd(); $fvManager_ImagePath = str_replace("/", "\\", $Bot_path); $iconurl = $fvManager_ImagePath . '\\' . $img['units_iconurl'] . ".40x40.jpeg"; $name = $fvManager_ImagePath . '\\' . str_replace("/", "\\", $img['units_iconurl']); if (!file_exists($name)) { return; } if (filesize($iconurl) == 0) { unlink($iconurl); } if (!file_exists($iconurl)) { if (file_exists($iconurl)) { return; } $arr = split("\\.", $name); $ext = $arr[count($arr) - 1]; $newext = "jpeg"; if ($ext == "jpeg" || $ext == "jpg") { $image = imagecreatefromjpeg($name); } elseif ($ext == "png") { $image = imagecreatefrompng($name); } elseif ($ext == "gif") { $image = imagecreatefromgif($name); } if (!$img) { return false; } $old_x = imageSX($image); $old_y = imageSY($image); if ($old_x < 40 && $old_y < 40) { $thumb_w = $old_x; $thumb_h = $old_y; } elseif ($old_x > $old_y) { $thumb_w = 40; $thumb_h = floor($old_y * (40 / $old_x)); } elseif ($old_x < $old_y) { $thumb_w = floor($old_x * (40 / $old_y)); $thumb_h = 40; } elseif ($old_x == $old_y) { $thumb_w = 40; $thumb_h = 40; } $thumb_w = $thumb_w < 1 ? 1 : $thumb_w; $thumb_h = $thumb_h < 1 ? 1 : $thumb_h; $new_img = ImageCreateTrueColor($thumb_w, $thumb_h); if ($transparency) { if ($ext == "png") { imagealphablending($new_img, false); $colorTransparent = imagecolorallocatealpha($new_img, 0, 0, 0, 127); imagefill($new_img, 0, 0, $colorTransparent); imagesavealpha($new_img, true); } elseif ($ext == "gif") { $trnprt_indx = imagecolortransparent($img); if ($trnprt_indx >= 0) { $trnprt_color = imagecolorsforindex($image, $trnprt_indx); $trnprt_indx = imagecolorallocate($new_img, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']); imagefill($new_img, 0, 0, $trnprt_indx); imagecolortransparent($new_img, $trnprt_indx); } } } else { Imagefill($new_img, 0, 0, imagecolorallocate($new_img, 255, 255, 255)); } imagecopyresampled($new_img, $image, 0, 0, 0, 0, $thumb_w, $thumb_h, $old_x, $old_y); if ($border) { $black = imagecolorallocate($new_img, 0, 0, 0); imagerectangle($new_img, 0, 0, $thumb_w, $thumb_h, $black); } imagejpeg($new_img, $iconurl, 50); imagedestroy($new_img); imagedestroy($image); } return; }
private function vtxResizeImagem($arquivo, $fotoNome, $base64 = false) { $fotoLargura = $this->larguraFoto; $fotoAltura = $this->alturaFoto; $fotoQualidade = $this->qualidadeFoto; $fotoNome = $fotoNome; $fotoOpacidade = $this->fotoTransparencia != "" ? $this->fotoTransparencia : FALSE; $gerar = $this->getImageInfo($arquivo); $old_x = $gerar['width']; $old_y = $gerar['height']; $img = $gerar['create']; /* # check props if($old_x < $fotoLargura && $old_y < $fotoAltura): $thumb_w = $old_x; $thumb_h = $old_y; elseif ($old_x > $old_y): $thumb_w = $fotoLargura; $thumb_h = floor(($old_y*($fotoAltura/$old_x))); elseif ($old_x < $old_y): $thumb_w = floor($old_x*($fotoLargura/$old_y)); $thumb_h = $fotoAltura; elseif ($old_x == $old_y): $thumb_w = $fotoLargura; $thumb_h = $fotoAltura; endif; $thumb_w = ($thumb_w<1) ? 1 : $thumb_w; $thumb_h = ($thumb_h<1) ? 1 : $thumb_h; $new_img = ImageCreateTrueColor($thumb_w, $thumb_h); */ ########################################################################### $ratio = $gerar['width'] / $gerar['height']; // width/height if ($ratio > 1) { $thumb_w = $this->larguraFoto; $thumb_h = $this->alturaFoto / $ratio; } else { $thumb_w = $this->larguraFoto * $ratio; $thumb_h = $this->alturaFoto; } # $src = imagecreatefromstring(file_get_contents($fn)); # $dst = imagecreatetruecolor($width,$height); # imagecopyresampled($dst,$src,0,0,0,0,$width,$height,$size[0],$size[1]); $new_img = ImageCreateTrueColor($thumb_w, $thumb_h); ########################################################################### if ($fotoOpacidade == false || $fotoOpacidade == 0 || $fotoOpacidade == "") { if ($gerar['mime'] == "image/png" || $gerar['mime'] == "image/x-png") { imagealphablending($new_img, false); $colorTransparent = imagecolorallocatealpha($new_img, 0, 0, 0, 127); imagefill($new_img, 0, 0, $colorTransparent); imagesavealpha($new_img, true); } elseif ($gerar['mime'] == "image/gif") { $trnprt_indx = imagecolortransparent($img); if ($trnprt_indx >= 0) { //its transparent $trnprt_color = imagecolorsforindex($img, $trnprt_indx); $trnprt_indx = imagecolorallocate($new_img, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']); imagefill($new_img, 0, 0, $trnprt_indx); imagecolortransparent($new_img, $trnprt_indx); } } } else { Imagefill($new_img, 0, 0, imagecolorallocate($new_img, 255, 255, 255)); } imagecopyresampled($new_img, $img, 0, 0, 0, 0, $thumb_w, $thumb_h, $old_x, $old_y); $fotoNome = !empty($fotoNome) ? $fotoNome : ''; if ($base64) { ob_start(); imagepng($new_img); $img = ob_get_contents(); ob_end_clean(); $return = base64_encode($img); } else { if ($gerar['mime'] == "image/jpeg" || $gerar['mime'] == "image/pjpeg") { if (!empty($fotoNome)) { imagejpeg($new_img, $fotoNome, $fotoQualidade); } else { imagejpeg($new_img, NULL, 75); } $return = true; } elseif ($gerar['mime'] == "image/png" || $gerar['mime'] == "image/x-png") { if (!empty($fotoNome)) { imagepng($new_img, $fotoNome); } else { imagepng($new_img, NULL); } $return = true; } elseif ($gerar['mime'] == "image/gif") { if (!empty($fotoNome)) { imagegif($new_img, $fotoNome, $fotoQualidade); } else { imagegif($new_img, NULL, 75); } $return = true; } } imagedestroy($new_img); imagedestroy($img); return $return; }
function Resize_Image($src, $width = 0, $height = 0, $dst) { if ($height <= 0 && $width <= 0) { return false; } // Setting defaults and meta $image = ''; $final_width = 0; $final_height = 0; list($width_old, $height_old, $image_type) = GetImageSize($src); // Calculating proportionality if ($width == 0) { $factor = $height / $height_old; } elseif ($height == 0) { $factor = $width / $width_old; } else { $factor = Min($width / $width_old, $height / $height_old); } $final_width = Round($width_old * $factor); $final_height = Round($height_old * $factor); // Loading image to memory according to type switch ($image_type) { case IMAGETYPE_GIF: $image = imagecreatefromgif($src); break; case IMAGETYPE_JPEG: $image = imagecreatefromjpeg($src); break; case IMAGETYPE_PNG: $image = imagecreatefrompng($src); break; default: return false; } // This is the resizing/resampling/transparency-preserving magic $image_resized = ImageCreateTrueColor($final_width, $final_height); if ($image_type == IMAGETYPE_GIF || $image_type == IMAGETYPE_PNG) { $transparency = ImageColorTransparent($image); if ($image_type == IMAGETYPE_GIF && $transparency >= 0) { list($r, $g, $b) = Array_Values(ImageColorsForIndex($image, $transparency)); $transparency = ImageColorAllocate($image_resized, $r, $g, $b); Imagefill($image_resized, 0, 0, $transparency); ImageColorTransparent($image_resized, $transparency); } elseif ($image_type == IMAGETYPE_PNG) { ImageAlphaBlending($image_resized, false); $color = ImageColorAllocateAlpha($image_resized, 0, 0, 0, 127); ImageFill($image_resized, 0, 0, $color); ImageSaveAlpha($image_resized, true); } } ImageCopyResampled($image_resized, $image, 0, 0, 0, 0, $final_width, $final_height, $width_old, $height_old); // Writing image switch ($image_type) { case IMAGETYPE_GIF: imagegif($image_resized, $dst); break; case IMAGETYPE_JPEG: imagejpeg($image_resized, $dst, 85); break; case IMAGETYPE_PNG: imagepng($image_resized, $dst); break; default: return false; } }
/** * Created by PhpStorm. * User: zhuangshaoxiong * Date: 2015/12/3 * Time: 20:19 */ $string = "abcdefghijklmnopqrstuvwxyz0123456789"; $str = ""; for ($i = 0; $i < 4; $i++) { $pos = rand(0, 35); $str .= $string[$pos]; } session_start(); $_SESSION['checknode'] = $str; $img_handle = Imagecreate(100, 20); $back_color = ImageColorAllocate($img_handle, 255, 255, 255); $txt_color = ImageColorAllocate($img_handle, 0, 0, 0); echo ddddd; for ($i = 0; $i < 3; $i++) { $line = ImageColorAllocate($img_handle, rand(0, 255), rand(0, 255), rand(0, 255)); Imageline($img_handle, rand(0, 15), rand(0, 15), rand(100, 150), rand(10, 50), $line); } for ($i = 0; $i < 200; $i++) { $randcolor = ImageColorallocate($img_handle, rand(0, 255), rand(0, 255), rand(0, 255)); Imagesetpixel($img_handle, rand() % 100, rand() % 50, $randcolor); } Imagefill($img_handle, 0, 0, $back_color); ImageString($img_handle, 28, 10, 0, $str, $txt_color); ob_clean(); header("Content-Type: image/png;"); Imagepng($img_handle);
function Resample_Image() { if ($this->dst_height <= 0 && $this->dst_width <= 0) { return False; } # Setting defaults and meta $image = $this->image; $final_width = 0; $final_height = 0; # Get File Information if ($arr_image_size = GetImageSize($this->attachment_file)) { list($width_old, $height_old, $image_type) = $arr_image_size; } else { return False; } if ($this->crop) { $factor = Max($this->dst_width / $width_old, $this->dst_height / $height_old); $final_width = $this->dst_width; $final_height = $this->dst_height; } else { # Calculating proportionality if ($this->dst_width == 0) { $factor = $this->dst_height / $height_old; } elseif ($this->dst_height == 0) { $factor = $this->dst_width / $width_old; } else { $factor = Min($this->dst_width / $width_old, $this->dst_height / $height_old); } $final_width = $width_old * $factor; $final_height = $height_old * $factor; } # Resample the image if (!Function_Exists('ImageCreateTrueColor')) { return False; } if (!($this->image = ImageCreateTrueColor($final_width, $final_height))) { return False; } # Copy the Transparency properties if ($image_type == IMAGETYPE_GIF || $image_type == IMAGETYPE_PNG) { if ($image_type == IMAGETYPE_GIF && $transparency >= 0) { $transparency = ImageColorTransparent($image); list($r, $g, $b) = Array_Values(ImageColorsForIndex($image, $transparency)); ImageColorAllocate($this->image, $r, $g, $b); Imagefill($this->image, 0, 0, $transparency); ImageColorTransparent($this->image, $transparency); } elseif ($image_type == IMAGETYPE_PNG) { ImageAlphaBlending($this->image, False); ImageSaveAlpha($this->image, True); } } if (!Function_Exists('ImageCopyResampled')) { return False; } if ($this->crop) { # Crop the image ImageCopyResampled($this->image, $image, 0, 0, ($width_old * $factor - $final_width) / (2 * $factor), ($height_old * $factor - $final_height) / (2 * $factor), $final_width, $final_height, $final_width / $factor, $final_height / $factor); // int $src_w , int $src_h } else { # Resample aspect ratio ImageCopyResampled($this->image, $image, 0, 0, 0, 0, $final_width, $final_height, $width_old, $height_old); } return True; }