使用PHP旋转然后裁剪图像

时间:2015-08-06 05:09:01

标签: php image

我试图裁剪我的图像,但是当我这样做时,它会显示为正确的尺寸,但全部为黑色。我已经尝试了至少十几个不同的脚本,但我似乎无法让它们中的任何一个工作:(

哦,旋转脚本工作正常,所有的回声都只是用于测试,将被删除:D

<?php

$staffID        =   $_POST['u'];
$actCode        =   $_POST['a'];
$tempAvatar     =   $_POST['tempAvatar'];
$x1             =   $_POST['x'];
$y1             =   $_POST['y'];
$wH             =   $_POST['w'];
$scale          =   $_POST['scale'];
$angle          =   $_POST['angle'];
$destFolder     =   "../../../avatars/";
$imagePath      =   "tempAvatars/".$tempAvatar.".jpg";
$imagePathRot   =   "tempAvatars/".$tempAvatar."ROTATED.jpg";
$imagePathCropped=  "tempAvatars/".$tempAvatar."CROPPED.jpg";

echo 'X1: '.$x1.'<br>Y1: '.$y1.'<br>Width/Height: '.$wH.'<br>Angle: '.$angle;
if ($angle != 0) {

    $source = imagecreatefromjpeg($imagePath) or notfound();
    $rotate = imagerotate($source,$angle,0);
    imagejpeg($rotate, $imagePathRot);

    $imagePath  =   $imagePathRot;
}

echo '<br>X2: '.$x2.'<br>Y2: '.$y2;

$targ_w = 300;
$jpeg_quality = 90;

$img_r = imagecreatefromjpeg($imagePath);
$dst_r = ImageCreateTrueColor( $targ_w, $targ_w );

imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'],
    $targ_w,$targ_h,$_POST['w'],$_POST['w']);

imagejpeg($dst_r, $imagePathCropped, $jpeg_quality);



echo '<br><img src="'.$imagePathCropped.'">';


?>

1 个答案:

答案 0 :(得分:2)

您的问题是package DataAccess; import DataAccess.dbconnector; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.sql.*; import java.util.logging.Level; import java.util.logging.Logger; @WebServlet(urlPatterns = {"/LoginServlet"}) public class LoginServlet extends HttpServlet { String userid, pwd; Connection connection = null; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); Statement statement = null; userid = request.getParameter("uname"); pwd = request.getParameter("pass"); try { dbconnector dbc = new dbconnector(); connection = dbc.Open(); PreparedStatement ps = connection.prepareStatement("select * from member where uname='" + userid + "' and pass='" + pwd + "'"); ResultSet rs = ps.executeQuery(); if (rs.next()) { HttpSession session = request.getSession(); session.setAttribute("userid", userid); response.sendRedirect("view/starter001.jsp"); } else { request.setAttribute("errorMessage", "Invalid user or password"); RequestDispatcher rd = getServletContext().getRequestDispatcher("/index.jsp"); rd.include(request, response); } } catch (Exception e) { e.printStackTrace(); } finally { try { connection.close(); } catch (SQLException ex) { Logger.getLogger(LoginServlet.class.getName()).log(Level.SEVERE, null, ex); } } } 未定义,因此您正在复制0像素&#34;行&#34;来自源图像。它的大小正确,因为它由$targ_h决定,当然也会初始化为黑色。根据代码的其余部分进行的正确调用应该是:

ImageCreateTrueColor