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); }
<?php list($width, $height) = getimagesize($_POST["imageSource"]); $viewPortW = $_POST["viewPortW"]; $viewPortH = $_POST["viewPortH"]; $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); $selectorX = $_POST["selectorX"]; $selectorY = $_POST["selectorY"]; if ($_POST["imageRotate"]) { $angle = 360 - $_POST["imageRotate"]; $image_p = imagerotate($image_p, $angle, 0); $pWidth = imagesx($image_p); $pHeight = imagesy($image_p); //print $pWidth."---".$pHeight; $diffW = abs($pWidth - $widthR) / 2; $diffH = abs($pHeight - $hegihtR) / 2; $_POST["imageX"] = $pWidth > $widthR ? $_POST["imageX"] - $diffW : $_POST["imageX"] + $diffW; $_POST["imageY"] = $pHeight > $hegihtR ? $_POST["imageY"] - $diffH : $_POST["imageY"] + $diffH;
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); }