如何在使用GD Library上传期间压缩和调整图像大小?

时间:2016-01-14 15:51:48

标签: php file-upload mysqli gd image-uploading

如何使用GD库在上传期间压缩和调整图像大小?

在当前代码中,尝试调整大小和压缩,但是我没能做到正确。上传的图片为黑色。此外,我可以上传相同的name.jpg文件,即使它已经存在,脚本也应该阻止它。

希望你能提供帮助,我会从中学到很多东西。

以下是代码:

// if the form's submit button is clicked, we need to process the form, but the logo is uploaded first
if (isset($_POST['submit']))
{
        // logo definition
        $logoname = $_FILES["logo"]["name"];
        $logotmp_name = $_FILES["logo"]["tmp_name"];
        $logotype = $_FILES["logo"]["type"];
        $logosize = $_FILES["logo"]["size"];
        $logoerror = $_FILES["logo"]["error"];
        $logofolder = "../images/";
        $logodestination = $logofolder . $logoname;
        // logo upload
        if(isset($logoerror)){ if($logoerror > 0){ echo "Error: " . $logoerror . "<br>"; } else {
        $allowed = array("jpg" => "image/jpg", "jpeg" => "image/jpeg", "gif" => "image/gif", "png" => "image/png");
        // verify logo extension
        $ext = pathinfo($logoname, PATHINFO_EXTENSION); if(!array_key_exists($ext, $allowed)) die("Error: Please select a valid file format.");    
        // verify logo size - 5MB maximum
        $maxsize = 5 * 1024 * 1024; if($logosize > $maxsize) die("Error: File size is larger than the allowed limit.");
        // verify MIME type of the logo
        if(in_array($logotype, $allowed)){
        // resize the image 
        list($width, $height) = getimagesize($logotmp_name);
        // make the new image width of 600
        $newwidth = 600;
        // make the new image height of 400
        $newheight = 400;
        // It loads the images we use jpeg function you can use any function like imagecreatefromjpeg
        $thumb = imagecreatetruecolor($newwidth, $newheight);
        $source = imagecreatefromjpeg($logodestination);
        // Resize the $thumb image.
        imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
        // It then save the new image to the location specified by $logoname variable
        imagejpeg($thumb, $logodestination, 75); 
        // verify whether the logo already exists before uploading it
        if(file_exists($logofolder . $logoname)){ echo $logoname . " is already exists."; } else {
        move_uploaded_file($logotmp_name, $logofolder . $logoname);

        echo "Your file was uploaded successfully."; } } else { 
        echo "Error: There was a problem uploading your file - please try again."; } } } else { 
        echo "Error: Invalid parameters - please contact your server administrator."; }
// get the form data and logo path
$rating = htmlentities($_POST['rating'], ENT_QUOTES);
$synopsis = htmlentities($_POST['synopsis'], ENT_QUOTES);
$age = htmlentities($_POST['age'], ENT_QUOTES);
$logo = "/images/".$logoname;
$active = htmlentities($_POST['active'], ENT_QUOTES);

// check that rating, synopsis, age, logo and active are not empty
if ($rating == '' || $synopsis == '' || $age == '' || $logo == '' || $active == '')
{
// if they are empty, show an error message and display the form
$error = 'ERROR: Please fill in all required fields!';
renderForm($rating, $synopsis, $age, $logo, $active, $error);
}
else
{
// insert the new record into the database
if ($stmt = $mysqli->prepare("INSERT age_ratings (rating, synopsis, age, logo, active) VALUES (?, ?, ?, ?, ?)"))
{
$stmt->bind_param("sssss", $rating, $synopsis, $age, $logo, $active);
$stmt->execute();
$stmt->close();
}
// show an error if the query has an error
else
{
echo "ERROR: Could not prepare SQL statement.";
}

0 个答案:

没有答案