function set_reasonable_image_size($image, $filename) { global $CONF; list($width, $height) = getimagesize($filename); if ($width <= $CONF['user_temp_avatar_max_width'] && $height <= $CONF['user_temp_avatar_max_height']) return $image; $size = resize_dimensions($CONF['user_temp_avatar_max_width'],$CONF['user_temp_avatar_max_height'],$width,$height); $fwidth = $size['width']; $fheight = $size['height']; $thumb = imagecreatetruecolor($fwidth, $fheight); setTransparency($thumb,$image); $index = imagecolorexact($thumb, 255, 255, 255); imagecolortransparent($thumb, $index); imagecopyresampled($thumb, $image, 0, 0, 0, 0, $fwidth, $fheight, $width, $height); imagepng($thumb,$filename); return $thumb; }
function update_user_avatar($file, $x1, $y1, $x2, $y2){ //file_put_contents('aa.txt',$file); preg_match('/\/(\d+\.png)$/', $file, $matches); // this is for security reasons $file = 'imgs/temp/'.$matches[1]; global $_FILES; global $_USER; global $CONF; $user = $_SESSION['user']; $result='ok'; $image = imagecreatefrompng($file); $thumbb = imagecreatetruecolor($CONF['user_avatar_width_big'], $CONF['user_avatar_width_big']); setTransparency($thumbb,$image); $thumbm = imagecreatetruecolor($CONF['user_avatar_width_med'], $CONF['user_avatar_width_med']); setTransparency($thumbm,$image); $thumbs = imagecreatetruecolor($CONF['user_avatar_width_small'], $CONF['user_avatar_width_small']);setTransparency($thumbs,$image); $index = imagecolorexact($thumbb, 255, 255, 255); imagecolortransparent($thumbb, $index); $index = imagecolorexact($thumbm, 255, 255, 255); imagecolortransparent($thumbm, $index); $index = imagecolorexact($thumbs, 255, 255, 255); imagecolortransparent($thumbs, $index); imagecopyresampled($thumbb, $image, 0, 0, $x1, $y1, $CONF['user_avatar_width_big'], $CONF['user_avatar_width_big'], $x2-$x1, $y2-$y1); imagepng($thumbb,$file."-big"); imagecopyresampled($thumbm, $image, 0, 0, $x1, $y1, $CONF['user_avatar_width_med'], $CONF['user_avatar_width_med'], $x2-$x1, $y2-$y1); imagepng($thumbm,$file."-med"); imagecopyresampled($thumbs, $image, 0, 0, $x1, $y1, $CONF['user_avatar_width_small'], $CONF['user_avatar_width_small'], $x2-$x1, $y2-$y1); imagepng($thumbs,$file."-small"); $user->setAvatarFile($file); $result = $user->save(); unlink($file); unlink($file."-big"); unlink($file."-med"); unlink($file."-small"); if ($result=='ok') return array('ok'=>true, 'error'=>''); else return array('ok'=>false, 'error'=>"$result"); }
function ResizeAndCrop($img_src, $params) { $ext = end(explode(".", $img_src)); $function = returnCorrectFunction($ext); $image = $function($img_src); $width = imagesx($image); $height = imagesy($image); // Resample first $image_p = imagecreatetruecolor($params[0], $params[1]); setTransparency($image, $image_p, $ext); imagecopyresampled($image_p, $image, 0, 0, 0, 0, $params[0], $params[1], $width, $height); imagedestroy($image); $pWidth = imagesx($image_p); $pHeight = imagesy($image_p); $viewport = imagecreatetruecolor($params[4], $params[5]); setTransparency($image_p, $viewport, $ext); imagecopy($viewport, $image_p, 0, 0, $params[2], $params[3], $params[4], $params[5]); imagedestroy($image_p); return parseImage($ext, $viewport, $file = null); }
function resize_keepratio($file, $dest, $newheight) { //add support for png and gif list($width, $height) = getimagesize($file); $r = $width / $height; $n_height = $newheight; $n_width = $r * $n_height; // Check MIME Type by yourself. $finfo = new finfo(FILEINFO_MIME_TYPE); if (false === ($ext = array_search($finfo->file($file), array('jpg' => 'image/jpeg', 'png' => 'image/png', 'gif' => 'image/gif'), true))) { return false; } switch ($ext) { case 'jpg': $src = imagecreatefromjpeg($file); $dst = imagecreatetruecolor($n_width, $n_height); break; case 'png': $src = imagecreatefrompng($file); $dst = imagecreatetruecolor($n_width, $n_height); setTransparency($dst, $src); break; case 'gif': $src = imagecreatefromgif($file); $dst = imagecreatetruecolor($n_width, $n_height); setTransparency($dst, $src); break; } imagecopyresampled($dst, $src, 0, 0, 0, 0, $n_width, $n_height, $width, $height); switch ($ext) { case 'jpg': return imagejpeg($dst, $dest, 95); break; case 'png': return imagepng($dst, $dest, 9); break; case 'gif': return imagegif($dst, $dest); break; } }
function ResizeImage($file, $max_width, $max_height) { if (is_file($file)) { $gis = getimagesize($file); $type = $gis[2]; switch ($type) { case "1": $imorig = imagecreatefromgif($file); break; case "2": $imorig = imagecreatefromjpeg($file); break; case "3": $imorig = imagecreatefrompng($file); break; default: $imorig = imagecreatefromjpeg($file); } $width = imagesx($imorig); $height = imagesy($imorig); $new_width = $width; $new_height = $height; if ($new_width > $max_width) { $new_width = $max_width; $new_height = $height * ($new_width / $width); } if ($new_height > $max_height) { $new_height = $max_height; $new_width = $width * ($new_height / $height); } $im = imagecreatetruecolor($new_width, $new_height); setTransparency($im, $imorig); imagecopyresampled($im, $imorig, 0, 0, 0, 0, $new_width, $new_height, $width, $height); imagejpeg($im, $file . '_resized'); } }
if ($h == 'auto' && is_numeric($w)) { $h = $w / $x * 100 * ($y / 100); if ($h > $ma) { $h = $ma; $w = $h / $y * 100 * ($x / 100); } } if ($w == 'auto' && is_numeric($h)) { $w = $h / $y * 100 * ($x / 100); if ($w > $ma) { $w = $ma; $h = $w / $x * 100 * ($y / 100); } } $im = imagecreatetruecolor($w, $h); setTransparency($im, $imorig); imagecopyresampled($im, $imorig, 0, 0, 0, 0, $w, $h, $x, $y); $cx = empty($_GET['cx']) ? 0 : $_GET['cx']; $cy = empty($_GET['cy']) ? 0 : $_GET['cy']; if (!empty($cx) && !empty($cy)) { $sx = ($cx - $w) / 2; $sy = ($cy - $h) / 2; $canvas = imagecreatetruecolor($cx, $cy); $white = imagecolorallocate($canvas, 255, 255, 255); imagefill($canvas, 0, 0, $white); imagecopy($canvas, $im, $sx, $sy, 0, 0, $w, $h); $im = $canvas; } header('Content-Type: image/jpeg'); imagejpeg($im); imagedestroy($im);
if ($_POST["imageX"] > 0) { $dst_x = abs($_POST["imageX"]); } else { $src_x = abs($_POST["imageX"]); } if ($_POST["imageY"] > 0) { $dst_y = abs($_POST["imageY"]); } else { $src_y = abs($_POST["imageY"]); } $viewport = imagecreatetruecolor($_POST["viewPortW"], $_POST["viewPortH"]); setTransparency($image_p, $viewport, $ext); imagecopy($viewport, $image_p, $dst_x, $dst_y, $src_x, $src_y, $pWidth, $pHeight); imagedestroy($image_p); $selector = imagecreatetruecolor($_POST["selectorW"], $_POST["selectorH"]); setTransparency($viewport, $selector, $ext); imagecopy($selector, $viewport, 0, 0, $selectorX, $selectorY, $_POST["viewPortW"], $_POST["viewPortH"]); $file = "uploads/o_" . $ext . time() . "." . $ext; parseImage($ext, $selector, $file); $fileName = $file; // Create image for 1920x1440 $this->convert_images($fileName, 1920, 1440); // Create image for 1680x1050 $this->convert_images($fileName, 1680, 1050); // Create image for 1440x900 $this->convert_images($fileName, 1440, 900); // Create image for 1366x768 $this->convert_images($fileName, 1366, 768); // Create image for 1280x1024 $this->convert_images($fileName, 1280, 1024); // Create image for 1024x768
function miniatura($prefixo, $img, $caminho, $onde_salvar, $modo_de_rdm, $largura, $altura, $qualidade = 85, $marca = 'nao') { //adiciona a imagem ao caminho $caminho = $caminho . $img; if (substr($caminho, 0, 3) == '../') { $volta = '../'; } else { $volta = ''; } //checa se imagem existe, se não joga o erro de não encontrada if (!file_exists($caminho) or empty($img)) { $caminho = $volta . 'imagens/imagem_nao_encontrada.gif'; $modo_de_rdm = 'esticar'; } //criar diretorio das miniaturas se nao existir if (!file_exists($onde_salvar . $prefixo)) { mkdir($onde_salvar . $prefixo, 0777, true); } $foto_peq = $onde_salvar . $prefixo . $img; if (file_exists($foto_peq) && !isset($_GET['substitui'])) { return $onde_salvar . $prefixo . $img; exit; } elseif (!file_exists($foto_peq) or isset($_GET['substitui'])) { //retorna informações da imagem list($largura_orig, $altura_orig) = getimagesize($caminho); if ($largura == 'auto' && $altura == 'auto') { $largura = $largura_orig; $altura = $altura_orig; } //pra deixar uma altura proporcinal ou fixa na imagem if ($altura == 'auto') { $proporcao = (double) $altura_orig / $largura_orig; $mini_altura = round($largura * $proporcao); } else { $mini_altura = $altura; } $altura = $mini_altura; //pra deixar a largura proporcinal ou fixa na imagem if ($largura == 'auto') { $proporcao = (double) $largura_orig / $altura_orig; $mini_largura = round($altura * $proporcao); } else { $mini_largura = $largura; } $largura = $mini_largura; if ($largura_orig < $largura) { $largura = $largura_orig; $altura = $altura_orig; $mini_largura = $largura; $mini_altura = $altura; } //detecta o tipo de arquivo $fonte = imagecreatefromstring(file_get_contents($caminho)); //cria canvas $mini = imagecreatetruecolor($largura, $altura); //para deixar as png com transpararencia $branco = imagecolorallocate($mini, 0, 0, 0); setTransparency($mini, $fonte); // imagealphablending( $mini, false ); // imagesavealpha( $mini, true ); //imagecolortransparent ($mini, $branco); //imagefill($mini, 0, 0, $branco); //calcula a proporção da altura da miniatura de acordo com a largura, para fazer as verificações $proporcao = (double) $altura_orig / $largura_orig; $verifica_altura = round($largura * $proporcao); if ($modo_de_rdm == 'cortar') { if ($verifica_altura == $altura) { imagecopyresampled($mini, $fonte, 0, 0, 0, 0, $mini_largura, $mini_altura, $largura_orig, $altura_orig); } else { if ($verifica_altura > $altura) { $corte = round(($verifica_altura - $altura) / 2); imagecopyresampled($mini, $fonte, 0, -$corte, 0, 0, $mini_largura, $verifica_altura, $largura_orig, $altura_orig); } else { $proporcao = (double) $largura_orig / $altura_orig; $mini_largura = round($altura * $proporcao); $aumenta = round(($mini_largura - $largura) / 2); $mostra = $largura; imagecopyresampled($mini, $fonte, -$aumenta, 0, 0, 0, $mini_largura, $mini_altura, $largura_orig, $altura_orig); } } //calcula a proporção da altura da miniatura de acordo com a largura, para fazer as verificações $proporcao = (double) $altura_orig / $largura_orig; $verifica_largura = round($altura * $proporcao); } if ($mini_largura == 'auto' or $mini_altura == 'auto' or $modo_de_rdm == 'esticar') { imagecopyresampled($mini, $fonte, 0, 0, 0, 0, $mini_largura, $mini_altura, $largura_orig, $altura_orig); } if ($marca == 'marca') { $marca = $volta . "imagens/marca.png"; $imagem_marca = imagecreatefrompng($marca); $preto = imagecolorallocate($imagem_marca, 0, 0, 0); imagecolortransparent($imagem_marca, $preto); $marca_larg = imagesx($imagem_marca); $marca_alt = imagesy($imagem_marca); imagecopyresampled($mini, $imagem_marca, $mini_largura - ($marca_larg + 10), $mini_altura - ($marca_alt + 10), 0, 0, $marca_larg, $marca_alt, $marca_larg, $marca_alt); } //destrói, para liberar memória imagedestroy($fonte); //cria thumb baseada no tipo da imagem if ($caminho == $volta . 'imagens/imagem_nao_encontrada.gif') { $miniatura = $onde_salvar . 'imagem_nao_encontrada.gif'; } else { if (strpos($img, '/') !== false) { $img = str_replace('/', '_', $img); } $miniatura = $onde_salvar . $prefixo . $img; } imagejpeg($mini, $miniatura, $qualidade); return $miniatura; exit; } }
} elseif ($width_source == $height_source) { // image carree $width_destination = $max_height; $height_destination = $width_destination; } else { // image verticale $height_destination = $max_height; $width_destination = floor($height_destination * $width_source / $height_source); } } else { $width_destination = $max_width; $height_destination = $max_height; } $destination = imagecreatetruecolor($width_destination, $height_destination); //image miniature vide cree setTransparency($destination, $source); imagecopyresampled($destination, $source, 0, 0, 0, 0, $width_destination, $height_destination, $width_source, $height_source); // On cree la miniature header("Content-type: " . $mime_type[$extension]); // envoi du header qui va bien selon le type switch ($extension) { case 'gif': imagegif($destination); // affichage dun gif break; case 'png': imagepng($destination); // affichage dun png break; case 'jpg': default:
function resize_and_crop() { error_log(print_r($_POST, true)); $uid = MooGetGPC('uid', 'integer'); list($width, $height) = getimagesize($_POST["imageSource"]); $pWidth = $_POST["imageW"]; $pHeight = $_POST["imageH"]; $ext = end(explode(".", $_POST["imageSource"])); $function = returnCorrectFunction($ext); $image = $function($_POST["imageSource"]); $width = imagesx($image); $height = imagesy($image); // Resample $image_p = imagecreatetruecolor($pWidth, $pHeight); setTransparency($image, $image_p, $ext); imagecopyresampled($image_p, $image, 0, 0, 0, 0, $pWidth, $pHeight, $width, $height); imagedestroy($image); $widthR = imagesx($image_p); $hegihtR = imagesy($image_p); if ($_POST["imageRotate"]) { $angle = 360 - $_POST["imageRotate"]; $image_p = imagerotate($image_p, $angle, 0); $pWidth = imagesx($image_p); $pHeight = imagesy($image_p); } if ($pWidth > $_POST["viewPortW"]) { $src_x = abs(abs($_POST["imageX"]) - abs(($_POST["imageW"] - $pWidth) / 2)); $dst_x = 0; } else { $src_x = 0; $dst_x = $_POST["imageX"] + ($_POST["imageW"] - $pWidth) / 2; } if ($pHeight > $_POST["viewPortH"]) { $src_y = abs($_POST["imageY"] - abs(($_POST["imageH"] - $pHeight) / 2)); $dst_y = 0; } else { $src_y = 0; $dst_y = $_POST["imageY"] + ($_POST["imageH"] - $pHeight) / 2; } $viewport = imagecreatetruecolor($_POST["viewPortW"], $_POST["viewPortH"]); setTransparency($image_p, $viewport, $ext); imagecopy($viewport, $image_p, $dst_x, $dst_y, $src_x, $src_y, $pWidth, $pHeight); imagedestroy($image_p); $selector = imagecreatetruecolor($_POST["selectorW"], $_POST["selectorH"]); setTransparency($viewport, $selector, $ext); imagecopy($selector, $viewport, 0, 0, $_POST["selectorX"], $_POST["selectorY"], $_POST["viewPortW"], $_POST["viewPortH"]); $file = "tmp/test" . time() . "." . $ext; parseImage($ext, $selector, $file); imagedestroy($viewport); //Return value echo $file; //写水印......fanglin $thumb_image_name = $file; $first = new Imagick($thumb_image_name); //写入水印 $second = new Imagick('../public/system/images/logo2.png'); $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); //imagick处理.......fanglin $bigphoto = $thumb_image_name; $thuid = $uid * 3; $userpath = "../data/upload/userimg/"; $jpg = $ext; $sizearray = array(0 => array('width' => 320, 'height' => 400), 1 => array('width' => 171, 'height' => 212), 2 => array('width' => 100, 'height' => 125), 3 => array('width' => 60, 'height' => 75)); $namearray = array(0 => 'big', 1 => 'mid', 2 => 'medium', 3 => 'small'); foreach ($sizearray as $k => $size) { $index_name = $thuid . '_' . $namearray[$k] . '.' . $jpg; ImagickResizeImage($bigphoto, $userpath . $index_name, $size['width'], $size['height']); } $index_name = $thuid . '_' . 'index.' . $jpg; $com_name = $thuid . '_' . 'com.' . $jpg; $page_name = $thuid . '_' . 'page.' . $jpg; list($width, $height) = getimagesize($bigphoto); $d = $width / $height; $c = 100 / 125; //100*125 if ($d > $c) { $g1_width = 100; $b = $width / $g1_width; $g1_height = $height / $b; } else { $g1_height = 125; $b = $height / $g1_height; $g1_width = $width / $b; } ImagickResizeImage($bigphoto, $userpath . $index_name, $g1_width, $g1_height); $c = 50 / 63; //100*125 if ($d > $c) { $g2_width = 50; $b = $width / $g2_width; $g2_height = $height / $b; } else { $g2_height = 63; $b = $height / $g2_height; $g2_width = $width / $b; } ImagickResizeImage($bigphoto, $userpath . $page_name, $g2_width, $g2_height); $c = 110 / 138; //100*125 if ($d > $c) { $g3_width = 110; $b = $width / $g3_width; $g3_height = $height / $b; } else { $g3_height = 138; $b = $height / $g3_height; $g3_width = $width / $b; } ImagickResizeImage($bigphoto, $userpath . $com_name, $g3_width, $g3_height); }