public function imprimir_ticket() { // Prueba de impresión de ticket directamente a impresora // Crea codigo de barras $barras_txt = '0123456790123'; $this->barcode->barcode_img_tipo = 'png'; $barras = ($barrasImg = $this->barcode->create($barras_txt)) ? true : false; #Crea jpg $barrasBmp = $barras ? imagebmp('png', $barrasImg, 'assets\\tmp\\barcode.bmp') : false; #Convierte jpg->bmp // Crea código QR $qr_txt = 'http://www.isolution.mx'; $qr = ($qrImg = $this->codeqr->create($qr_txt)) ? true : false; $qrBmp = $qr ? imagebmp('png', $qrImg, 'assets\\tmp\\qrcode.bmp') : false; #Convierte jpg->bmp // Envía datos $impData = array('contenido' => 'assets\\tmp\\ticket.txt', 'logo' => 'assets/images/logo.bmp', 'impresora' => 'PDFCreator', 'formato' => true, 'codebar' => $barrasBmp, 'codeqr' => $qrBmp); // Imprime ticket if ($this->impresion->enviar_a_impresora($impData)) { echo "Impresión enviada: " . date('Y-m-d H:i:s'); } // Elimina imagenes generadas if ($barrasImg) { unlink($barrasImg); } if ($barrasBmp) { unlink($barrasBmp); } if ($qrImg) { unlink($qrImg); } if ($qrBmp) { unlink($qrBmp); } }
function save($handle, $uri = null) { if ($uri == null) { imagebmp($handle); } else { imagebmp($handle, $uri); } }
function img_thumb($target, $newcopy, $w, $h, $ext) { list($w_orig, $h_orig) = getimagesize($target); // get image sizes of the uploaded image $ratio_orig = $w_orig / $h_orig; // width ratio // figure out max height or width if ($w / $h > $ratio_orig) { $w = $h * $ratio_orig; } else { $h = $w / $ratio_orig; } $ext = strtolower($ext); $img = ""; if ($ext == "gif") { $img = imagecreatefromgif($target); } else { if ($ext == "png") { $img = imagecreatefrompng($target); } else { $img = imagecreatefromjpeg($target); } } $tci = imagecreatetruecolor($w, $h); if ($ext == "gif" or $ext == "png") { imagecolortransparent($tci, imagecolorallocatealpha($tci, 0, 0, 0, 127)); imagealphablending($tci, false); imagesavealpha($tci, true); } imagecopyresampled($tci, $img, 0, 0, 0, 0, $w, $h, $w_orig, $h_orig); // if($src_x<0) { // $bgd = imagecolorallocate($tci, 255, 255, 255); // imagefill($tci, 0, 0, $bgd); // } if ($ext == "gif") { imagegif($tci, $newcopy); } else { if ($ext == "png") { imagepng($tci, $newcopy); } else { if ($ext == "bmp") { imagebmp($tci, $newcopy); } else { imagejpeg($tci, $newcopy, 84); } } } }
function helper_watermark($name, $ext) { ($hook = kleeja_run_hook('helper_watermark_func')) ? eval($hook) : null; //run hook #is this file really exsits ? if (!file_exists($name)) { return; } $src_logo = $logo_path = false; if (file_exists(dirname(__FILE__) . '/../../images/watermark.png')) { $logo_path = dirname(__FILE__) . '/../../images/watermark.png'; $src_logo = imagecreatefrompng($logo_path); } elseif (file_exists(dirname(__FILE__) . '/../../images/watermark.gif')) { $logo_path = dirname(__FILE__) . '/../../images/watermark.gif'; $src_logo = imagecreatefromgif($logo_path); } #no watermark pic if (!$src_logo) { return; } #if there is imagick lib, then we should use it if (function_exists('phpversion') && phpversion('imagick')) { helper_watermark_imagick($name, $ext, $logo_path); return; } #now, lets work and detect our image extension if (strpos($ext, 'jp') !== false) { $src_img = @imagecreatefromjpeg($name); } elseif (strpos($ext, 'png') !== false) { $src_img = @imagecreatefrompng($name); } elseif (strpos($ext, 'gif') !== false) { return; $src_img = @imagecreatefromgif($name); } elseif (strpos($ext, 'bmp') !== false) { if (!defined('BMP_CLASS_INCLUDED')) { include dirname(__FILE__) . '/BMP.php'; define('BMP_CLASS_INCLUDED', true); } $src_img = imagecreatefrombmp($name); } else { return; } #detect width, height for the image $bwidth = @imageSX($src_img); $bheight = @imageSY($src_img); #detect width, height for the watermark image $lwidth = @imageSX($src_logo); $lheight = @imageSY($src_logo); if ($bwidth > $lwidth + 5 && $bheight > $lheight + 5) { #where exaxtly do we have to make the watermark .. $src_x = $bwidth - ($lwidth + 5); $src_y = $bheight - ($lheight + 5); #make it now, watermark it @ImageAlphaBlending($src_img, true); @ImageCopy($src_img, $src_logo, $src_x, $src_y, 0, 0, $lwidth, $lheight); if (strpos($ext, 'jp') !== false) { @imagejpeg($src_img, $name); } elseif (strpos($ext, 'png') !== false) { @imagepng($src_img, $name); } elseif (strpos($ext, 'gif') !== false) { @imagegif($src_img, $name); } elseif (strpos($ext, 'bmp') !== false) { @imagebmp($src_img, $name); } } else { #image is not big enough to watermark it return false; } }
public static function imagebmp(&$img, $filename = false) { return imagebmp($img, $filename); }
function makeThumbWatermark($width = 128, $height = 128) { $this->fileCheck(); $image_info = $this->getInfo($this->src_image_name); if (!$image_info) { return false; } $src_image_type = $image_info["type"]; $img = $this->createImage($src_image_type, $this->src_image_name); if (!$img) { return false; } $width = $width == 0 ? $image_info["width"] : $width; $height = $height == 0 ? $image_info["height"] : $height; $width = $width > $image_info["width"] ? $image_info["width"] : $width; $height = $height > $image_info["height"] ? $image_info["height"] : $height; $srcW = $image_info["width"]; $srcH = $image_info["height"]; if ($srcH * $width > $srcW * $height) { $width = round($srcW * $height / $srcH); } else { $height = round($srcH * $width / $srcW); } //* $src_image = @imagecreatetruecolor($width, $height); $white = @imagecolorallocate($src_image, 0xff, 0xff, 0xff); @imagecolortransparent($src_image, $white); @imagefilltoborder($src_image, 0, 0, $white, $white); if ($src_image) { ImageCopyResampled($src_image, $img, 0, 0, 0, 0, $width, $height, $image_info["width"], $image_info["height"]); } else { $src_image = imagecreate($width, $height); ImageCopyResized($src_image, $img, 0, 0, 0, 0, $width, $height, $image_info["width"], $image_info["height"]); } $src_image_w = ImageSX($src_image); $src_image_h = ImageSY($src_image); if ($this->wm_image_name) { $wm_image_info = $this->getInfo($this->wm_image_name); if (!$wm_image_info) { return false; } $wm_image_type = $wm_image_info["type"]; $wm_image = $this->createImage($wm_image_type, $this->wm_image_name); $wm_image_w = ImageSX($wm_image); $wm_image_h = ImageSY($wm_image); $temp_wm_image = $this->getPos($src_image_w, $src_image_h, $this->wm_image_pos, $wm_image); if ($this->emboss && function_exists("imagefilter")) { imagefilter($wm_image, IMG_FILTER_EMBOSS); $bgcolor = imagecolorclosest($wm_image, 0x7f, 0x7f, 0x7f); imagecolortransparent($wm_image, $bgcolor); } if (function_exists("ImageAlphaBlending") && IMAGETYPE_PNG == $wm_image_info['type']) { ImageAlphaBlending($src_image, true); } $wm_image_x = $temp_wm_image["dest_x"]; $wm_image_y = $temp_wm_image["dest_y"]; if (IMAGETYPE_PNG == $wm_image_info['type']) { imageCopy($src_image, $wm_image, $wm_image_x, $wm_image_y, 0, 0, $wm_image_w, $wm_image_h); } else { imageCopyMerge($src_image, $wm_image, $wm_image_x, $wm_image_y, 0, 0, $wm_image_w, $wm_image_h, $this->wm_image_transition); } } if ($this->wm_text) { $this->wm_text = $this->wm_text; $temp_wm_text = $this->getPos($src_image_w, $src_image_h, $this->wm_image_pos); $wm_text_x = $temp_wm_text["dest_x"]; $wm_text_y = $temp_wm_text["dest_y"]; if (preg_match("/([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])/i", $this->wm_text_color, $color)) { $red = hexdec($color[1]); $green = hexdec($color[2]); $blue = hexdec($color[3]); $wm_text_color = imagecolorallocate($src_image, $red, $green, $blue); } else { $wm_text_color = imagecolorallocate($src_image, 255, 255, 255); } imagettftext($src_image, $this->wm_text_size, $this->wm_angle, $wm_text_x, $wm_text_y, $wm_text_color, $this->wm_text_font, $this->wm_text); } if ($this->save_file) { switch ($src_image_type) { case 1: if ($this->gif_enable) { $src_img = ImageGIF($src_image, $this->save_file); } else { $src_img = ImagePNG($src_image, $this->save_file); } break; case 2: $src_img = ImageJPEG($src_image, $this->save_file, $this->jpeg_quality); break; case 3: $src_img = ImagePNG($src_image, $this->save_file); break; default: $src_img = ImageJPEG($src_image, $this->save_file, $this->jpeg_quality); break; } } else { switch ($src_image_type) { case 1: if ($this->gif_enable) { header("Content-type: image/gif"); $src_img = ImageGIF($src_image); } else { header("Content-type: image/png"); $src_img = ImagePNG($src_image); } break; case 2: header("Content-type: image/jpeg"); $src_img = ImageJPEG($src_image, "", $this->jpeg_quality); break; case 3: header("Content-type: image/png"); $src_img = ImagePNG($src_image); break; case 6: header("Content-type: image/bmp"); $src_img = imagebmp($src_image); break; default: header("Content-type: image/jpeg"); $src_img = ImageJPEG($src_image, "", $this->jpeg_quality); break; } } imagedestroy($src_image); imagedestroy($img); return true; }
//print_r($data); // Rotate $degrees = 90; $rotate = imagerotate($im, $degrees, 0); switch ($type) { case 1: imagegif($rotate, $filepath, 100); break; case 2: imagejpeg($rotate, $filepath, 100); break; case 3: imagepng($rotate, $filepath, 100); break; case 6: imagebmp($rotate, $filepath, 100); break; } imagedestroy($im); imagedestroy($rotate); } } ?> <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title> Profile </title> <meta name="mobile-web-app-capable" content="yes">
/** * [resizeImage description] * @param [type] $im [源目标图片] * @param [type] $maxwidth [最大宽度] * @param [type] $maxheight [最大高度] * @param [type] $name [图片名] * @param [type] $filetype [图片类型] * @param [type] $tmp_name [上传的文件的临时路径] * @return [type] [成功true] */ function resizeImage($tmp_name, $maxwidth, $maxheight, $name, $filetype) { try { $img_info = getimagesize($tmp_name); if (!in_array($img_info['mime'], array('image/jpeg', 'image/png', 'image/bmp', 'image/gif', 'image/pjpeg', 'image/jpg', 'image/x-png'))) { $this->errmsg = '只支持上传图片'; return FALSE; } $pic_width = $img_info[0]; $pic_height = $img_info[1]; if ($maxwidth && $pic_width > $maxwidth || $maxheight && $pic_height > $maxheight) { $resizeheightTag = $resizewidthTag = false; if ($maxwidth && $pic_width > $maxwidth) { $widthratio = $maxwidth / $pic_width; $resizewidthTag = true; } if ($maxheight && $pic_height > $maxheight) { $heightratio = $maxheight / $pic_height; $resizeheightTag = true; } if ($resizewidthTag && $resizeheightTag) { if ($widthratio < $heightratio) { $ratio = $widthratio; } else { $ratio = $heightratio; } } if ($resizewidthTag && !$resizeheightTag) { $ratio = $widthratio; } if ($resizeheightTag && !$resizewidthTag) { $ratio = $heightratio; } $newwidth = $pic_width * $ratio; $newheight = $pic_height * $ratio; $newim = imagecreatetruecolor($newwidth, $newheight); switch ($img_info['mime']) { case "image/gif": $images = imagecreatefromgif($tmp_name); break; case "image/pjpeg": case "image/jpeg": case "image/jpg": $images = imagecreatefromjpeg($tmp_name); break; case "image/png": case "image/x-png": $images = imagecreatefrompng($tmp_name); break; case "image/bmp": $images = imageCreateFromBmp($tmp_name); break; } imagecopyresampled($newim, $images, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height); $name = $this->save_path . $name . $filetype; switch ($img_info['mime']) { case "image/gif": imagegif($newim, $name); break; case "image/pjpeg": case "image/jpeg": case "image/jpg": imagejpeg($newim, $name, 100); break; case "image/png": case "image/x-png": imagepng($newim, $name); break; case "image/bmp": imagebmp($newim, $name); break; } imagedestroy($newim); } else { if (!copy($tmp_name, $this->save_path . $name . $filetype)) { return false; } } return TRUE; } catch (E $e) { return FALSE; } }
public function to($src) { imagebmp($this->image, $src); }
function ImageResize($srcFile, $toW, $toH, $toFile = "") { global $cfg_photo_type; if ($toFile == '') { $toFile = $srcFile; } $info = ''; $srcInfo = GetImageSize($srcFile, $info); switch ($srcInfo[2]) { case 1: if (!$cfg_photo_type['gif']) { return FALSE; } $im = imagecreatefromgif($srcFile); break; case 2: if (!$cfg_photo_type['jpeg']) { return FALSE; } $im = imagecreatefromjpeg($srcFile); break; case 3: if (!$cfg_photo_type['png']) { return FALSE; } $im = imagecreatefrompng($srcFile); break; case 6: if (!$cfg_photo_type['bmp']) { return FALSE; } $im = imagecreatefromwbmp($srcFile); break; } $srcW = ImageSX($im); $srcH = ImageSY($im); if ($srcW <= $toW && $srcH <= $toH) { return TRUE; } $toWH = $toW / $toH; $srcWH = $srcW / $srcH; if ($toWH <= $srcWH) { $ftoW = $toW; $ftoH = $ftoW * ($srcH / $srcW); } else { $ftoH = $toH; $ftoW = $ftoH * ($srcW / $srcH); } if ($srcW > $toW || $srcH > $toH) { if (function_exists("imagecreateTRUEcolor")) { @($ni = imagecreateTRUEcolor($ftoW, $ftoH)); if ($ni) { imagecopyresampled($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH); } else { $ni = imagecreate($ftoW, $ftoH); imagecopyresized($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH); } } else { $ni = imagecreate($ftoW, $ftoH); imagecopyresized($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH); } switch ($srcInfo[2]) { case 1: imagegif($ni, $toFile); break; case 2: imagejpeg($ni, $toFile, 85); break; case 3: imagepng($ni, $toFile); break; case 6: imagebmp($ni, $toFile); break; default: return FALSE; } imagedestroy($ni); } imagedestroy($im); return TRUE; }
/** * replaces all pictures of $this-user with a dummy for the given object-type * @param int $object_type object_types-id from table object_types * @return bool true, if replacement worked, false otherwise */ function replace_pictures($object_type) { // get optionsarray global $opt; // load bmp-support require_once $opt['rootpath'] . 'lib2/imagebmp.inc.php'; // paths cleared by trailing '/' if (substr($opt['logic']['pictures']['dir'], -1) != '/') { $picpath = $opt['logic']['pictures']['dir']; } else { $picpath = substr($opt['logic']['pictures']['dir'], 0, -1); } $thumbpath = "{$picpath}/thumbs"; $pdummy = isset($opt['logic']['pictures']['dummy']); if ($pdummy && isset($opt['logic']['pictures']['dummy']['bgcolor']) && is_array($opt['logic']['pictures']['dummy']['bgcolor'])) { $dummybg = $opt['logic']['pictures']['dummy']['bgcolor']; } else { $dummybg = array(255, 255, 255); } if ($pdummy && isset($opt['logic']['pictures']['dummy']['text'])) { $dummytext = $opt['logic']['pictures']['dummy']['text']; } else { $dummytext = ''; } if ($pdummy && isset($opt['logic']['pictures']['dummy']['textcolor']) && is_array($opt['logic']['pictures']['dummy']['textcolor'])) { $dummytextcolor = $opt['logic']['pictures']['dummy']['textcolor']; } else { $dummytextcolor = array(0, 0, 0); } $tmh = 0; $tmw = 0; /* * check log or cache */ if ($object_type == OBJECT_CACHE) { // get filenames of the pictures of $this' caches $rs = sql("SELECT `pictures`.`url` " . "FROM `pictures`,`caches` " . "WHERE `caches`.`cache_id`=`pictures`.`object_id`" . " AND `pictures`.`object_type`='&1' AND `caches`.`user_id`='&2'", OBJECT_CACHE, $this->getUserId()); } elseif ($object_type == OBJECT_CACHELOG) { // get filenames of the pictures of $this' logs $rs = sql("SELECT `pictures`.`url` " . "FROM `pictures`,`cache_logs` " . "WHERE `cache_logs`.`id`=`pictures`.`object_id`" . " AND `pictures`.`object_type`='&1' AND `cache_logs`.`user_id`='&2'", OBJECT_CACHELOG, $this->getUserId()); } // set thumb-dimensions $tmh = $opt['logic']['pictures']['thumb_max_height']; $tmw = $opt['logic']['pictures']['thumb_max_width']; $filenames = array(); while ($url = sql_fetch_array($rs, MYSQL_NUM)) { $filenames[] = substr($url['url'], -40); } // free result sql_free_result($rs); /* * walk through filenames and replace original */ // check if there is something to replace if (count($filenames) > 0) { foreach ($filenames as $fn) { // get uuid and extension $uuid = substr($fn, 0, 36); $ext = substr($fn, -3); $thumb_dir1 = substr($uuid, 0, 1); $thumb_dir2 = substr($uuid, 1, 1); // read original size if (file_exists("{$picpath}/{$fn}")) { list($w, $h, $t, $attr) = getimagesize("{$picpath}/{$fn}"); } else { $w = 600; $h = 480; } // create new image $im = imagecreatetruecolor($w, $h); // allocate colors $col_bg = imagecolorallocate($im, $dummybg[0], $dummybg[1], $dummybg[2]); $col_text = imagecolorallocate($im, $dummytextcolor[0], $dummytextcolor[1], $dummytextcolor[2]); // fill bg imagefill($im, 0, 0, $col_bg); // check for replacement-image if ($pdummy && isset($opt['logic']['pictures']['dummy']['replacepic']) && $opt['logic']['pictures']['dummy']['replacepic'] != $opt['rootpath'] . 'images/' && file_exists($opt['logic']['pictures']['dummy']['replacepic'])) { // get dimensions of the replacement list($rw, $rh, $rt, $rattr) = getimagesize($opt['logic']['pictures']['dummy']['replacepic']); $rwh = 0; if ($rw > $rh) { $rwh = $rh; } else { $rwh = $rw; } // check dimensions of original and set replacement size $rsize = 0; if ($w > $h) { if ($h * 0.85 > $rwh) { $rsize = $rwh; } else { $rsize = $h * 0.9; } } else { if ($w * 0.85 > $rwh) { $rsize = $rwh; } else { $rsize = $w * 0.9; } } $dx = ($w - $rsize) / 2; $dy = ($h - $rsize) / 2; // get replacement image $rext = substr($opt['logic']['pictures']['dummy']['replacepic'], -3); $rim = null; if ($rext == 'jpg') { $rim = imagecreatefromjpeg($opt['logic']['pictures']['dummy']['replacepic']); } elseif ($rext == 'png') { $rim = imagecreatefrompng($opt['logic']['pictures']['dummy']['replacepic']); } elseif ($rext == 'gif') { $rim = imagecreatefromgif($opt['logic']['pictures']['dummy']['replacepic']); } elseif ($rext == 'bmp') { $rim = imagecreatefrombmp($opt['logic']['pictures']['dummy']['replacepic']); } // copy image if (!is_null($rim)) { imagecopyresampled($im, $rim, $dx, $dy, 0, 0, $rsize, $rsize, $rw, $rh); } } else { // set text if ($dummytext != '') { imagestring($im, 1, 10, $h / 2, $dummytext, $col_text); } else { imageline($im, 0, 0, $w, $h, 0xff0000); imageline($im, 0, $h, $w, 0, 0xff0000); } } // save dummy if ($ext == 'jpg') { if (!imagejpeg($im, "{$picpath}/{$fn}", 75)) { return "save dummy failed [{$ext}]"; } } elseif ($ext == 'png') { if (!imagepng($im, "{$picpath}/{$fn}", 4)) { return "save dummy failed [{$ext}]"; } } elseif ($ext == 'gif') { if (!imagegif($im, "{$picpath}/{$fn}")) { return "save dummy failed [{$ext}]"; } } elseif ($ext == 'bmp') { if (!imagebmp($im, "{$picpath}/{$fn}")) { return "save dummy failed [{$ext}]"; } } else { return "save dummy failed [{$ext}], unknown extension"; } // set thumb-dimensions if ($h > $tmh || $w > $tmw) { if ($h > $w) { $th = $tmh; $tw = $w * ($th / $h); } else { $tw = $tmw; $th = $h * ($tw / $w); } } else { $tw = $w; $th = $h; } // copy dummy $tim = imagecreatetruecolor($tw, $th); imagecopyresampled($tim, $im, 0, 0, 0, 0, $tw, $th, $w, $h); // check directories or create them if (!file_exists("{$thumbpath}/{$thumb_dir1}")) { if (!mkdir("{$thumbpath}/{$thumb_dir1}")) { return 'mkdir in thumbpath failed'; } } if (!file_exists("{$thumbpath}/{$thumb_dir1}/{$thumb_dir2}")) { if (!mkdir("{$thumbpath}/{$thumb_dir1}/{$thumb_dir2}")) { return 'mkdir in thumbpath failed'; } } // save thumb if ($ext == 'jpg') { if (!imagejpeg($tim, "{$thumbpath}/{$thumb_dir1}/{$thumb_dir2}/{$fn}", 75)) { return "save thumb failed [{$ext}]"; } } elseif ($ext == 'png') { if (!imagepng($tim, "{$thumbpath}/{$thumb_dir1}/{$thumb_dir2}/{$fn}", 4)) { return "save thumb failed [{$ext}]"; } } elseif ($ext == 'gif') { if (!imagegif($tim, "{$thumbpath}/{$thumb_dir1}/{$thumb_dir2}/{$fn}")) { return "save thumb failed [{$ext}]"; } } elseif ($ext == 'bmp') { if (!imagebmp($tim, "{$thumbpath}/{$thumb_dir1}/{$thumb_dir2}/{$fn}")) { return "save thumb failed [{$ext}]"; } } else { return "save thumb failed [{$ext}], unknown extension"; } } } // success return true; }
$img = imagecreatefromjpeg($fil); $new_image_ext = 'jpg'; } if ($sorthvid == "1") { imagefilter($img, IMG_FILTER_GRAYSCALE); } imagefilter($img, IMG_FILTER_BRIGHTNESS, $lysbalance); imagefilter($img, IMG_FILTER_CONTRAST, $kontrast); switch ($type) { case 'jpeg': imagejpeg($img, $fil); break; case 'png': imagepng($img, $fil); break; case 'bmp': imagebmp($img, $fil); break; case 'gif': imagegif($img, $fil); break; case 'vnd.wap.wbmp': imagewbmp($img, $fil); break; case 'xbm': imagexbm($img, $fil); break; default: imagejpeg($img, $fil); } imagedestroy($img);
} else { $newheight = $height; } $destimg = @imagecreatetruecolor(100, $newheight); imagecopyresampled($destimg, $orig_image, 0, 0, 0, 0, 100, $newheight, imagesx($orig_image), imagesy($orig_image)); if ($ext == 'jpg' or $ext == 'jpeg') { @imagejpeg($destimg, $dl->url . $thumb); } else { if ($ext == 'png') { @imagepng($destimg, $dl->url . $thumb); } else { if ($ext == 'gif') { @imagegif($destimg, $dl->url . $thumb); } else { if ($ext == 'bmp') { @imagebmp($destimg, $dl->url . $thumb); } } } } @imagedestroy($destimg); $db->query_write("INSERT INTO " . TABLE_PREFIX . "dl_images (`file`,`name`,`thumb`,`uploader`,`uploaderid`,`date`) VALUES(" . $file['id'] . "," . $db->sql_prepare($newfilename) . "," . $db->sql_prepare($thumb) . "," . $db->sql_prepare($vbulletin->userinfo['username']) . "," . $vbulletin->userinfo['userid'] . "," . $db->sql_prepare(TIMENOW) . ")"); eval(print_standard_redirect('ecdownloads_msg_image_added', true, true)); } } if ($_GET['act'] == 'delimg') { $image = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "dl_images WHERE `id` = " . $db->sql_prepare($_GET['img'])); if ($permissions['ecdownloadpermissions'] & $vbulletin->bf_ugp['ecdownloadpermissions']['caneditallfiles'] or $permissions['ecdownloadpermissions'] & $vbulletin->bf_ugp['ecdownloadpermissions']['caneditownfiles'] and ($image['uploaderid'] == $vbulletin->userinfo['userid'] and $file['uploaderid'] == $vbulletin->userinfo['userid'])) { $db->query_write("DELETE FROM " . TABLE_PREFIX . "dl_images WHERE `id` = " . $db->sql_prepare($image['id'])); @unlink($dl->url . $image['name']); @unlink($dl->url . $image['thumb']);
function img_thumb($target, $newcopy, $w, $h, $ext) { list($w_orig, $h_orig) = getimagesize($target); // get image sizes of the uploaded image $wr = $w_orig / $w; // get the width ratio $hr = $h_orig / $h; // get the height ratio $src_x = 0; // start our crop x source at 0 $src_y = 0; // start our crop y source at 0 // figure out if we need to crop the height or width to match our destination size ratio if ($hr < $wr) { // perform if the height is the limiting factor $ow = $w_orig; // store our original width for later use $w_orig = $w * $hr; // new original width $src_x = ($ow - $w_orig) / 2; // our new value to center crop the width } if ($wr < $hr) { // perform if the width is the limiting factor $oh = $h_orig; // store our original width for later use $h_orig = $h * $wr; // new original height $src_y = ($oh - $h_orig) / 2; // our new value to center crop the height } $ext = strtolower($ext); $img = ""; if ($ext == "gif") { $img = imagecreatefromgif($target); } else { if ($ext == "png") { $img = imagecreatefrompng($target); } else { $img = imagecreatefromjpeg($target); } } $tci = imagecreatetruecolor($w, $h); if ($ext == "gif" or $ext == "png") { imagecolortransparent($tci, imagecolorallocatealpha($tci, 0, 0, 0, 127)); imagealphablending($tci, false); imagesavealpha($tci, true); } imagecopyresampled($tci, $img, 0, 0, $src_x, $src_y, $w, $h, $w_orig, $h_orig); // if($src_x<0) { // $bgd = imagecolorallocate($tci, 255, 255, 255); // imagefill($tci, 0, 0, $bgd); // } if ($ext == "gif") { imagegif($tci, $newcopy); } else { if ($ext == "png") { imagepng($tci, $newcopy); } else { if ($ext == "bmp") { imagebmp($tci, $newcopy); } else { imagejpeg($tci, $newcopy, 84); } } } }
function check_imagick_rotate() { $pic_path = MooGetGPC('pic_path', 'string', 'G'); $id = MooGetGPC('id', 'integer', 'G'); $uid = MooGetGPC('uid', 'integer', 'G'); $degrees = 90; $file = explode('.', $pic_path); // var_dump($file);exit; $new_file = '../' . $file[0] . '.' . $file[1]; $pic_path = '../' . $pic_path; $source = imagecreatefromjpeg($pic_path); $rotate = imagerotate($source, $degrees, 0); // var_dump($pic_path); list($imagewidth, $imageheight, $imageType) = getimagesize($pic_path); $imageType = image_type_to_mime_type($imageType); switch ($imageType) { case "image/gif": imagegif($rotate, $new_file, 100); break; case "image/pjpeg": case "image/jpeg": case "image/jpg": imagejpeg($rotate, $new_file, 100); break; case "image/png": case "image/x-png": imagepng($rotate, $new_file, 100); break; case "image/bmp": imagebmp($rotate, $new_file, 100); break; } //var_dump($new_file);exit; header('location:index.php?action=check&h=photo&type=show&id=100004&uid=100004'); }
function changesize($image, $path, $x, $y, $width, $height, $uid, $sizearray, $namearray) { list($imagewidth, $imageheight, $imageType) = getimagesize($image); $imageType = image_type_to_mime_type($imageType); $userpath = $uid * 3; if (!file_exists($path)) { mkdir($path, 0777); } for ($i = 0; $i < count($sizearray); $i++) { $new_Image = imagecreatetruecolor($sizearray[$i]['width'], $sizearray[$i]['height']); switch ($imageType) { case "image/gif": $source = imagecreatefromgif($image); $thumb_image_name = $path . "/" . $userpath . "_" . $namearray[$i] . ".gif"; break; case "image/pjpeg": case "image/jpeg": case "image/jpg": $source = imagecreatefromjpeg($image); $thumb_image_name = $path . "/" . $userpath . "_" . $namearray[$i] . ".jpg"; break; case "image/png": case "image/x-png": $source = imagecreatefrompng($image); $thumb_image_name = $path . "/" . $userpath . "_" . $namearray[$i] . ".png"; break; default: exit("不支持该图片格式:{$imageType} ,请禁止通过该图片的审核"); /* case "image/x-bmp": case "image/x-ms-bmp": case "image/bmp": $source=ImageCreateFromBMP($image); $thumb_image_name=$path."/".$userpath."_".$namearray[$i].".bmp"; */ } imagecopyresampled($new_Image, $source, 0, 0, $x, $y, $sizearray[$i]['width'], $sizearray[$i]['height'], $width, $height); switch ($imageType) { case "image/gif": imagegif($new_Image, $thumb_image_name); break; case "image/pjpeg": case "image/jpeg": case "image/jpg": imagejpeg($new_Image, $thumb_image_name, 90); break; case "image/png": case "image/x-png": imagepng($new_Image, $thumb_image_name); break; case "image/x-bmp": case "image/x-ms-bmp": case "image/bmp": imagebmp($new_Image, $thumb_image_name); break; } /*$first = new Imagick($thumb_image_name);//写入水印 $second = new Imagick('../public/system/images/logo_xxz.png'); $second->setImageOpacity (0.4);//设置透明度 $dw = new ImagickDraw(); $dw->setGravity(Imagick::GRAVITY_SOUTHEAST);//设置位置 $dw->composite($second->getImageCompose(),0,0,50,0,$second); $first->drawImage($dw); $first->writeImage($thumb_image_name);*/ chmod($thumb_image_name, 0777); } return true; }
/** * Creates a resized image * @example createthumb('pics/apple.jpg','thumbs/tn_apple.jpg',100,100); */ function helper_thumb($source_path, $ext, $dest_image, $dw, $dh) { #no file, quit it if (!file_exists($source_path)) { return; } #check width, height if (intval($dw) == 0 || intval($dw) < 10) { $dw = 100; } if (intval($dh) == 0 || intval($dh) < 10) { $dh = $dw; } #if there is imagick lib, then we should use it if (function_exists('phpversion') && phpversion('imagick')) { helper_thumb_imagick($source_path, $ext, $dest_image, $dw, $dh); return; } //get file info list($source_width, $source_height, $source_type) = array(false, false, false); if (function_exists('getimagesize')) { list($source_width, $source_height, $source_type) = getimagesize($source_path); } if (!function_exists('imagecreatefromjpeg')) { return; } switch (strtolower(preg_replace('/^.*\\./', '', $source_path))) { case 'gif': $source_gdim = imagecreatefromgif($source_path); break; case 'jpg': case 'jpeg': $source_gdim = imagecreatefromjpeg($source_path); break; case 'png': $source_gdim = imagecreatefrompng($source_path); break; case 'bmp': if (!defined('BMP_CLASS_INCLUDED')) { include dirname(__FILE__) . '/BMP.php'; define('BMP_CLASS_INCLUDED', true); } $source_gdim = imagecreatefrombmp($source_path); break; } $source_width = !$source_width ? ImageSX($source_gdim) : $source_width; $source_height = !$source_height ? ImageSY($source_gdim) : $source_height; $source_aspect_ratio = $source_width / $source_height; $desired_aspect_ratio = $dw / $dh; if ($source_aspect_ratio > $desired_aspect_ratio) { // Triggered when source image is wider $temp_height = $dh; $temp_width = (int) ($dh * $source_aspect_ratio); } else { // Triggered otherwise (i.e. source image is similar or taller) $temp_width = $dw; $temp_height = (int) ($dw / $source_aspect_ratio); } // Resize the image into a temporary GD image $temp_gdim = imagecreatetruecolor($temp_width, $temp_height); imagecopyresampled($temp_gdim, $source_gdim, 0, 0, 0, 0, $temp_width, $temp_height, $source_width, $source_height); // Copy cropped region from temporary image into the desired GD image $x0 = ($temp_width - $dw) / 2; $y0 = ($temp_height - $dh) / 2; $desired_gdim = imagecreatetruecolor($dw, $dh); imagecopy($desired_gdim, $temp_gdim, 0, 0, $x0, $y0, $dw, $dh); // Create thumbnail switch (strtolower(preg_replace('/^.*\\./', '', $dest_image))) { case 'jpg': case 'jpeg': $return = @imagejpeg($desired_gdim, $dest_image, 90); break; case 'png': $return = @imagepng($desired_gdim, $dest_image); break; case 'gif': $return = @imagegif($desired_gdim, $dest_image); break; case 'bmp': $return = @imagebmp($desired_gdim, $dest_image); break; default: // Unsupported format $return = false; break; } @imagedestroy($desired_gdim); @imagedestroy($src_img); return $return; }
public static function resizePicCrop($input, $output, $width, $height, $raz) { /*if (file_exists ( $output )) unlink ( $output );*/ $size = getimagesize($input); $w = $size[0]; $h = $size[1]; if ($height == '') { $height = $h; } if ($width == '') { $width = $w; } if ($w > $h) { if ($h < $height) { $a = 1; $newy = $h; } else { $a = $height / $h; $newy = $height; } $newx = $a * $w; } else { if ($w < $width) { $a = 1; $newx = $w; } else { $a = $width / $w; $newx = $width; } $newy = $a * $h; } if ($newx < $width) { if ($w < $width) { $a = 1; $newx = $w; } else { $a = $width / $w; $newx = $width; } $newy = $a * $h; } if ($newy < $height) { if ($h < $height) { $a = 1; $newy = $h; } else { $a = $height / $h; $newy = $height; } $newx = $a * $w; } $source = imagecreatefromstring(file_get_contents($input)) or die('Cannot load original Image'); $target = imagecreatetruecolor($newx, $newy); imagealphablending($target, true); imagecopyresampled($target, $source, 0, 0, 0, 0, $newx, $newy, $size[0], $size[1]); if ($newx < $width) { $width = $newx; } if ($newy < $height) { $height = $newy; } $left = 0; $top = 0; if ($newx - $width > 0) { $left = ($newx - $width) / 2; } if ($newy - $height > 0) { $top = ($newy - $height) / 2; } //$source = imagecreatefromstring(file_get_contents($input)) or die ( 'Cannot load original Image' ); $target2 = imagecreatetruecolor($width, $height); imagealphablending($target2, true); imagecopy($target2, $target, 0, 0, $left, $top, $width, $height) or die('Cannot copy'); if ($raz == "png") { imagepng($target2, $output); } elseif ($raz == "gif") { imagegif($target2, $output); } elseif ($raz == "bmp") { imagebmp($target2, $output); } else { imagejpeg($target2, $output); } imagedestroy($target); imagedestroy($target2); imagedestroy($source); }
function create($filename = "") { if ($filename) { $this->src_image_name = strtolower(trim($filename)); } $src_image_type = $this->get_type($this->src_image_name); $src_image = $this->createImage($this->src_image_name, $src_image_type); if (!$src_image) { return; } $src_image_w = imagesx($src_image); $src_image_h = imagesy($src_image); if ($this->wm_image_name) { $this->wm_image_name = strtolower(trim($this->wm_image_name)); $wm_image_type = $this->get_type($this->wm_image_name); $wm_image = $this->createImage($this->wm_image_name, $wm_image_type); $wm_image_w = imagesx($wm_image); $wm_image_h = imagesy($wm_image); $temp_wm_image = $this->getPos($src_image_w, $src_image_h, $this->wm_image_pos, $wm_image); $wm_image_x = $temp_wm_image["dest_x"]; $wm_image_y = $temp_wm_image["dest_y"]; imagecopymerge($src_image, $wm_image, $wm_image_x, $wm_image_y, 0, 0, $wm_image_w, $wm_image_h, $this->wm_image_transition); } if ($this->wm_text) { $this->wm_text = $this->txt2utf8($this->wm_text, $this->codepage); $temp_wm_text = $this->getPos($src_image_w, $src_image_h, $this->wm_text_pos); $wm_text_x = $temp_wm_text["dest_x"]; $wm_text_y = $temp_wm_text["dest_y"]; if (preg_match("/([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])/i", $this->wm_text_color, $color)) { $red = hexdec($color[1]); $green = hexdec($color[2]); $blue = hexdec($color[3]); $wm_text_color = imagecolorallocate($src_image, $red, $green, $blue); } else { $wm_text_color = imagecolorallocate($src_image, 255, 255, 255); } imagettftext($src_image, $this->wm_text_size, $this->wm_angle, $wm_text_x, $wm_text_y, $wm_text_color, $this->wm_text_font, $this->wm_text); } if ($this->save_image_file) { switch ($src_image_type) { case 'gif': $src_img = imagegif($src_image, $this->save_image_file); break; case 'bmp': include_once dirname(__FILE__) . '/gdbmp.php'; $src_img = imagebmp($src_image, $this->save_image_file); break; case 'jpeg': $src_img = imagejpeg($src_image, $this->save_image_file, $this->jpeg_quality); break; case 'png': $src_img = imagepng($src_image, $this->save_image_file); break; default: $src_img = imagejpeg($src_image, $this->save_image_file, $this->jpeg_quality); break; } } else { if ($src_image_type = "jpg") { $src_image_type = "jpeg"; } header("Content-type: image/{$src_image_type}"); switch ($src_image_type) { case 'gif': $src_img = imagegif($src_image); break; case 'bmp': include_once dirname(__FILE__) . '/gdbmp.php'; $src_img = imagebmp($src_image); break; case 'jpg': $src_img = imagejpeg($src_image, "", $this->jpeg_quality); break; case 'png': $src_img = imagepng($src_image); break; default: $src_img = imagejpeg($src_image, "", $this->jpeg_quality); break; } } imagedestroy($src_image); }
if (!file_exists($opt['logic']['pictures']['thumb_dir'] . '/' . mb_substr($filename, 0, 1) . '/' . mb_substr($filename, 1, 1))) { mkdir($opt['logic']['pictures']['thumb_dir'] . '/' . mb_substr($filename, 0, 1) . '/' . mb_substr($filename, 1, 1)); } $savedir = $opt['logic']['pictures']['thumb_dir'] . '/' . mb_substr($filename, 0, 1) . '/' . mb_substr($filename, 1, 1); switch ($extension) { case 'jpg': imagejpeg($thumbimage, $savedir . '/' . $filename); break; case 'gif': imagegif($thumbimage, $savedir . '/' . $filename); break; case 'png': imagepng($thumbimage, $savedir . '/' . $filename); break; case 'bmp': imagebmp($thumbimage, $savedir . '/' . $filename); break; } sql("UPDATE `pictures` SET `thumb_last_generated`=NOW(), `thumb_url`='&1' WHERE `uuid`='&2'", $opt['logic']['pictures']['thumb_url'] . '/' . mb_substr($filename, 0, 1) . '/' . mb_substr($filename, 1, 1) . '/' . $filename, $r['uuid']); if ($debug == 1) { die($opt['logic']['pictures']['thumb_url'] . '/' . $filename); } else { $tpl->redirect(use_current_protocol($opt['logic']['pictures']['thumb_url'] . '/' . mb_substr($filename, 0, 1) . '/' . mb_substr($filename, 1, 1) . '/' . $filename)); } } else { if ($debug == 1) { die($r['thumb_url']); } else { $tpl->redirect(use_current_protocol($r['thumb_url'])); } }
@header("Content-type: image/jpeg"); @imagejpeg($img); } else { if ($ext == 'png') { @header("Content-type: image/png"); @imagepng($img); # Only if your version of GD includes GIF support } else { if ($ext == 'gif') { @header("Content-type: image/gif"); @imagegif($img); # Only if your version of GD includes GIF support } else { if ($ext == 'bmp') { @header("Content-type: image/bmp"); @imagebmp($img); # Only if your version of GD includes GIF support } else { if ($ext == 'tif') { @header("Content-type: image/tif"); @imagetif($img); # Only if your version of GD includes GIF support } } } } } } else { print "{$image_path}"; } /**
public function updateImage1($picname,$path,$prix="s_",$maxwidth=104,$maxheight=104){ //1. 定义获取基本信息 $path = rtrim($path,"/"); //去除后面多余的"/" $info1 = getimagesize($path."/".$picname); //获取图片文件的属性信息 $width = $info1[0]; //原图片的宽度 $height = $info1[1]; //原图片的高度 //2. 计算压缩后的尺寸 if(($maxwidth/$width)<($maxheight/$height)){ $w=$maxwidth;//新图片的宽度 $h=($maxwidth/$width)*$height;//新图片的高度 }else{ $h=$maxheight;//新图片的宽度 $w=($maxheight/$height)*$width;//新图片的高度 } //3. 创建图像源 $newim =imagecreateTrueColor($w,$h); //新图片源 //根据原图片类型来创建图片源 switch($info1[2]){ case 1: //gif格式 $srcim = imageCreateFromgif($path."/".$picname); break; case 2: //jpeg格式 $srcim = imageCreateFromjpeg($path."/".$picname); break; case 3: //png格式 $srcim = imageCreateFrompng($path."/".$picname); break; case 6: //bmp格式 $srcim = imageCreateFrompng($path."/".$picname); break; default: exit("无效的图片格式!"); break; } //4. 执行缩放处理 imagecopyresampled($newim,$srcim,0,0,0,0,$w,$h,$width,$height); //5. 输出保存绘画 //header("Content-Type:".$info['mime']); //设置响应类型为图片格式 //根据原图片类型来保存新图片 switch($info1[2]){ case 1: //gif格式 imagegif($newim,$path."/".$prix.$picname); //保存 //imagegif($newim);//输出 break; case 2: //jpeg格式 imagejpeg($newim,$path."/".$prix.$picname); //imagejpeg($newim); break; case 3: //png格式 imagepng($newim,$path."/".$prix.$picname); //imagepng($newim); break; case 6: //bmp格式 imagebmp($newim,$path."/".$prix.$picname); break; } //6. 销毁资源 imageDestroy($newim); imageDestroy($srcim); }
} } } $word2_contrast_ratio = $word2_black_pixel / $word2_white_pixel; if ($word1_contrast_ratio < $selective_contrast_threshold) { $word1_contrast++; } if ($word2_contrast_ratio < $selective_contrast_threshold) { $word2_contrast++; } if ($word1_contrast_ratio < $selective_contrast_threshold || $word2_contrast_ratio < $selective_contrast_threshold) { imagedestroy($contrast_img); goto a; } imagedestroy($source_img); imagebmp($contrast_img, 'contrast.bmp'); # resize image *************************************************************** # **************************************************************************** //imagebmp($sharp_img,"contrast.bmp"); $h2qx_output = shell_exec($h2qx_commandline); $img = imagecreatefrombmp('resized.bmp'); $height = imagesy($img); $width = imagesx($img); # sharpen ******************************************************************** # **************************************************************************** $sharp_img = imagecreatetruecolor($width, $height); imagecopy($sharp_img, $img, 0, 0, 0, 0, $width, $height); imagedestroy($img); if ($enable_sharpen) { for ($surrounding_pixel_threshold = $surrounding_pixel_threshold_max; $surrounding_pixel_threshold > $surrounding_pixel_threshold_min; $surrounding_pixel_threshold--) { for ($x = 1; $x < $width - 1; $x++) {
private function save($filename = '', $image_type = '', $compression = 75, $permissions = null) { if ($filename == '') { $filename = $this->filename; } if ($image_type == '') { $image_type = $this->image_type; } switch ($image_type) { case IMAGETYPE_JPEG: imagejpeg($this->image, $filename, $compression); break; case IMAGETYPE_GIF: $index = imagecolorexact($this->image, 0, 0, 0); imagegif($this->image, $filename); break; case IMAGETYPE_PNG: $index = imagecolorexact($this->image, 0, 0, 0); imagecolortransparent($this->image, $index); imagepng($this->image, $filename); break; case IMAGETYPE_BMP: imagebmp($this->image, $filename); break; } if ($permissions != null) { chmod($filename, $permissions); } }
/** * @brief Outputs/saves the image. */ private function _output($filepath = null) { if ($filepath) { if (!file_exists(dirname($filepath))) { mkdir(dirname($filepath), 0777, true); } if (!is_writable(dirname($filepath))) { OC_Log::write('core', __METHOD__ . '(): Directory \'' . dirname($filepath) . '\' is not writable.', OC_Log::ERROR); return false; } elseif (is_writable(dirname($filepath)) && file_exists($filepath) && !is_writable($filepath)) { OC_Log::write('core', __METHOD__ . '(): File \'' . $filepath . '\' is not writable.', OC_Log::ERROR); return false; } } if (!$this->valid()) { return false; } $retval = false; switch ($this->imagetype) { case IMAGETYPE_GIF: $retval = imagegif($this->resource, $filepath); break; case IMAGETYPE_JPEG: $retval = imagejpeg($this->resource, $filepath); break; case IMAGETYPE_PNG: $retval = imagepng($this->resource, $filepath); break; case IMAGETYPE_XBM: $retval = imagexbm($this->resource, $filepath); break; case IMAGETYPE_WBMP: $retval = imagewbmp($this->resource, $filepath); break; case IMAGETYPE_BMP: $retval = imagebmp($this->resource, $filepath, $this->bit_depth); break; default: $retval = imagepng($this->resource, $filepath); } return $retval; }
/** * 会对空白地方填充满 * * @access public * @param string $srcfile 图片路径 * @param string $towidth 转换到的宽度 * @param string $toheight 转换到的高度 * @param string $tofile 输出文件到 * @param string $issave 是否保存 * @param string $issource 是否保留原图 * @return bool */ function ImageResize($srcfile, $towidth, $toheight, $tofile = '', $issave = TRUE, $issource = TRUE) { global $cfg_imgresize; //如果不需要存储到它处 //直接覆盖原来文件位置 if ($tofile == '') { $tofile = $srcfile; } //获取图片信息 $srcinfo = @getimagesize($srcfile); //检测图片扩展名 if ($srcinfo[2] != 1 && $srcinfo[2] != 2 && $srcinfo[2] != 3 && $srcinfo[2] != 6) { return FALSE; } switch ($srcinfo[2]) { case 1: $imgfrom = imagecreatefromgif($srcfile); $extname = '.gif'; $newexts = '_hd.gif'; break; case 2: $imgfrom = imagecreatefromjpeg($srcfile); $extname = '.jpg'; $newexts = '_hd.jpg'; break; case 3: $imgfrom = imagecreatefrompng($srcfile); $extname = '.png'; $newexts = '_hd.png'; break; case 6: $imgfrom = imagecreatefromwbmp($srcfile); $extname = '.bmp'; $newexts = '_hd.bmp'; break; } //如果保留原图先将原图另存 if ($issource) { $srcfile_hd = str_replace($extname, $newexts, $srcfile); @copy($srcfile, $srcfile_hd); } //获取图片宽高 $imgwidth = $srcinfo[0]; $imgheight = $srcinfo[1]; if (!$imgwidth || !$imgheight) { return FALSE; } //判断缩略方式 //裁切方式 if ($cfg_imgresize == 'Y') { //宽高大于目标高度 if ($imgwidth > $towidth && $imgheight > $toheight) { //目标图片比例 $toratio = $towidth / $toheight; //当前图片比例 $imgratio = $imgwidth / $imgheight; //如果目标比例大于当前比例定义高度 if ($toratio > $imgratio) { $newwidth = $towidth; $newheight = $towidth / $imgratio; } else { $newwidth = $imgratio * $toheight; $newheight = $toheight; } //创建真彩色图像 $newimg = imagecreatetruecolor($towidth, $toheight); //缩放并合并图像 if (!@imagecopyresampled($newimg, $imgfrom, ($towidth - $newwidth) / 2, ($toheight - $newheight) / 2, 0, 0, $newwidth, $newheight, $imgwidth, $imgheight)) { return FALSE; } } else { if ($imgwidth >= $towidth && $imgheight <= $toheight) { $newwidth = $towidth; $newheight = $imgheight; //创建一张真彩图像 $newimg = imagecreatetruecolor($newwidth, $newheight); //裁切图像 if (!@imagecopyresampled($newimg, $imgfrom, ($towidth - $newwidth) / 2, 0, 0, 0, $newwidth, $newheight, $imgwidth, $imgheight)) { return FALSE; } } else { if ($imgwidth <= $towidth && $imgheight >= $toheight) { $newwidth = $imgwidth; $newheight = $toheight; //创建一张真彩图像 $newimg = imagecreatetruecolor($newwidth, $newheight); //裁切图像 if (!@imagecopyresampled($newimg, $imgfrom, 0, ($toheight - $newheight) / 2, 0, 0, $newwidth, $newheight, $imgwidth, $imgheight)) { return FALSE; } } else { if ($imgwidth <= $towidth && $imgheight <= $toheight) { imagedestroy($newimg); imagedestroy($imgfrom); return TRUE; } } } } } else { //目标图片比例 $toratio = $towidth / $toheight; //当前图片比例 $imgratio = $imgwidth / $imgheight; //如果目标比例大于当前比例定义高度 if ($toratio > $imgratio) { $newheight = $toheight; $newwidth = $imgratio * $toheight; } else { $newheight = $towidth / $imgratio; $newwidth = $towidth; } //匹配最终宽高 if ($newwidth > $towidth) { $newheight = $towidth; } if ($newheight > $toheight) { $newheight = $toheight; } //创建真彩色图像 $newimg = imagecreatetruecolor($towidth, $toheight); //为一幅图像分配颜色 $bgcolor = imagecolorallocate($newimg, 0xff, 0xff, 0xff); //画一矩形并填充 if (!@imagefilledrectangle($newimg, 0, 0, $towidth - 1, $toheight - 1, $bgcolor)) { return FALSE; } //缩放并合并图像 if (!@imagecopyresampled($newimg, $imgfrom, ($towidth - $newwidth) / 2, ($toheight - $newheight) / 2, 0, 0, $newwidth, $newheight, $imgwidth, $imgheight)) { return FALSE; } } //保存为目标文件 if ($issave) { switch ($srcinfo[2]) { case 1: imagegif($newimg, $tofile); break; case 2: imagejpeg($newimg, $tofile, 100); break; case 3: imagepng($newimg, $tofile); break; case 6: imagebmp($newimg, $tofile); break; default: return FALSE; } } else { switch ($srcinfo[2]) { case 1: imagegif($newimg); break; case 2: imagejpeg($newimg); break; case 3: imagepng($newimg); break; case 6: imagebmp($newimg); break; default: return FALSE; } } //销毁图像 imagedestroy($newimg); imagedestroy($imgfrom); return TRUE; }
private function new_img() { $ratio = $this->width / $this->height; //原图比例 $resize_ratio = $this->resize_width / $this->resize_height; //缩略后比例 $newimg = imagecreatetruecolor($this->resize_width, $this->resize_height); //生成新图片 if ($this->tag == 0) { //按固定高宽截取缩略图 $newimg = imagecreatetruecolor($this->resize_width, $this->resize_height); //生成新图片 if ($ratio >= $resize_ratio) { //即等比例下,缩略图的高比原图长,因此高不变 imagecopyresampled($newimg, $this->tem_file, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->height * $resize_ratio, $this->height); } elseif ($ratio < $resize_ratio) { //即等比例下,缩略图的宽比原图长,因此宽不变 imagecopyresampled($newimg, $this->tem_file, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width, $this->width / $resize_ratio); } } elseif ($this->tag == 1) { //按固定比例或最大长度缩小 if ($this->sca_max < 1) { //按比例缩小 $newimg = imagecreatetruecolor($this->width * $this->sca_max, $this->height * $this->sca_max); //生成新图片 imagecopyresampled($newimg, $this->tem_file, 0, 0, 0, 0, $this->width * $this->sca_max, $this->height * $this->sca_max, $this->width, $this->height); } elseif ($this->sca_max > 1) { //按某个最大长度缩小 if ($ratio >= 1) { //宽比高长 $newimg = imagecreatetruecolor($this->sca_max, $this->sca_max / $ratio); //生成新图片 imagecopyresampled($newimg, $this->tem_file, 0, 0, 0, 0, $this->sca_max, $this->sca_max / $ratio, $this->width, $this->height); } else { $newimg = imagecreatetruecolor($this->sca_max * $ratio, $this->sca_max); //生成新图片 imagecopyresampled($newimg, $this->tem_file, 0, 0, 0, 0, $this->sca_max * $ratio, $this->sca_max, $this->width, $this->height); } } } elseif ($this->tag == -1) { //按某个宽度或某个高度缩小 if ($resize_ratio >= 1) { //新高小于新宽,则图片按新宽度缩小 $newimg = imagecreatetruecolor($this->resize_width, $this->resize_width / $ratio); //生成新图片 imagecopyresampled($newimg, $this->tem_file, 0, 0, 0, 0, $this->resize_width, $this->resize_width / $ratio, $this->width, $this->height); } elseif ($resize_ratio < 1) { //新宽小于新高,则图片按新高度缩小 $newimg = imagecreatetruecolor($this->resize_height * $ratio, $this->resize_height); //生成新图片 imagecopyresampled($newimg, $this->tem_file, 0, 0, 0, 0, $this->resize_height * $ratio, $this->resize_height, $this->width, $this->height); } } //输出新图片 if ($this->type == 'jpeg' || $this->type == 'jpg') { imagejpeg($newimg, $this->des_file); } elseif ($this->type == 'gif') { imagegif($newimg, $this->des_file); } elseif ($this->type == 'png') { imagepng($newimg, $this->des_file); } elseif ($this->type == 'bmp') { imagebmp($newimg, $this->des_file); //bmp.php中包含 } }
<?php include dirname(__FILE__) . '/_init.php'; // 32bits/pixel BMP will be created from the truecolor image which has alpha channel. imagebmp(imagecreatefrompng('images/rgba-32bit.png'), 'output/rgba-32bit.bmp'); // 24bits/pixel BMP will be created from the truecolor image. imagebmp(imagecreatefrompng('images/rgb-24bit.png'), 'output/rgb-24bit.bmp'); // 8bits/pixel BMP will be created from the indexed color image (> 4bits/pizel). imagebmp(imagecreatefromgif('images/rgba-8bit.gif'), 'output/rgb-8bit.bmp'); // 4bits/pixel BMP will be created from the indexed color image (<= 4bits/pixel). imagebmp(imagecreatefromgif('images/rgb-4bit.gif'), 'output/rgb-4bit.bmp'); // 1bit/pixel BMP will be created from the indexed color image (= 1bit/pixel). imagebmp(imagecreatefromwbmp('images/mono.wbmp'), 'output/mono.bmp');
/** * @brief Outputs/saves the image. */ private function _output($filePath = null, $mimeType = null) { if ($filePath) { if (!file_exists(dirname($filePath))) { mkdir(dirname($filePath), 0777, true); } if (!is_writable(dirname($filePath))) { OC_Log::write('core', __METHOD__ . '(): Directory \'' . dirname($filePath) . '\' is not writable.', OC_Log::ERROR); return false; } elseif (is_writable(dirname($filePath)) && file_exists($filePath) && !is_writable($filePath)) { OC_Log::write('core', __METHOD__ . '(): File \'' . $filePath . '\' is not writable.', OC_Log::ERROR); return false; } } if (!$this->valid()) { return false; } $imageType = $this->imageType; if ($mimeType !== null) { switch ($mimeType) { case 'image/gif': $imageType = IMAGETYPE_GIF; break; case 'image/jpeg': $imageType = IMAGETYPE_JPEG; break; case 'image/png': $imageType = IMAGETYPE_PNG; break; case 'image/x-xbitmap': $imageType = IMAGETYPE_XBM; break; case 'image/bmp': $imageType = IMAGETYPE_BMP; break; default: throw new Exception('\\OC_Image::_output(): "' . $mimeType . '" is not supported when forcing a specific output format'); } } switch ($imageType) { case IMAGETYPE_GIF: $retVal = imagegif($this->resource, $filePath); break; case IMAGETYPE_JPEG: $retVal = imagejpeg($this->resource, $filePath); break; case IMAGETYPE_PNG: $retVal = imagepng($this->resource, $filePath); break; case IMAGETYPE_XBM: if (function_exists('imagexbm')) { $retVal = imagexbm($this->resource, $filePath); } else { throw new Exception('\\OC_Image::_output(): imagexbm() is not supported.'); } break; case IMAGETYPE_WBMP: $retVal = imagewbmp($this->resource, $filePath); break; case IMAGETYPE_BMP: $retVal = imagebmp($this->resource, $filePath, $this->bitDepth); break; default: $retVal = imagepng($this->resource, $filePath); } return $retVal; }
function ImageResizeNew($srcFile, $toW, $toH, $toFile = '', $issave = TRUE) { global $cfg_photo_type, $cfg_ddimg_bgcolor; if ($toFile == '') { $toFile = $srcFile; } $info = ''; $srcInfo = GetImageSize($srcFile, $info); switch ($srcInfo[2]) { case 1: if (!$cfg_photo_type['gif']) { return FALSE; } $img = imagecreatefromgif($srcFile); break; case 2: if (!$cfg_photo_type['jpeg']) { return FALSE; } $img = imagecreatefromjpeg($srcFile); break; case 3: if (!$cfg_photo_type['png']) { return FALSE; } $img = imagecreatefrompng($srcFile); break; case 6: if (!$cfg_photo_type['bmp']) { return FALSE; } $img = imagecreatefromwbmp($srcFile); break; } $width = imageSX($img); $height = imageSY($img); if (!$width || !$height) { return FALSE; } $target_width = $toW; $target_height = $toH; $target_ratio = $target_width / $target_height; $img_ratio = $width / $height; if ($target_ratio > $img_ratio) { $new_height = $target_height; $new_width = $img_ratio * $target_height; } else { $new_height = $target_width / $img_ratio; $new_width = $target_width; } if ($new_height > $target_height) { $new_height = $target_height; } if ($new_width > $target_width) { $new_height = $target_width; } $new_img = ImageCreateTrueColor($target_width, $target_height); if ($cfg_ddimg_bgcolor == 0) { $bgcolor = ImageColorAllocate($new_img, 0xff, 0xff, 0xff); } else { $bgcolor = 0; } if (!@imagefilledrectangle($new_img, 0, 0, $target_width - 1, $target_height - 1, $bgcolor)) { return FALSE; } if (!@imagecopyresampled($new_img, $img, ($target_width - $new_width) / 2, ($target_height - $new_height) / 2, 0, 0, $new_width, $new_height, $width, $height)) { return FALSE; } //保存为目标文件 if ($issave) { switch ($srcInfo[2]) { case 1: imagegif($new_img, $toFile); break; case 2: imagejpeg($new_img, $toFile, 100); break; case 3: imagepng($new_img, $toFile); break; case 6: imagebmp($new_img, $toFile); break; default: return FALSE; } } else { switch ($srcInfo[2]) { case 1: imagegif($new_img); break; case 2: imagejpeg($new_img); break; case 3: imagepng($new_img); break; case 6: imagebmp($new_img); break; default: return FALSE; } } imagedestroy($new_img); imagedestroy($img); return TRUE; }