在上传过程中减少php中的图像大小

时间:2014-10-18 08:34:31

标签: php gd

我试图在上传到我的数据库时减小图像的大小。但问题是,当我上传时,大小仍然保持不变。我已将我的代码附在任何帮助下面,感激地接受了。谢谢你

<?php
include ('connection.php');
$status="<div></div>";
if ($_SERVER["REQUEST_METHOD"] == "POST"){
    if($_FILES["photo_upload"]["size"]!=0){
        require 'image_compressor.php';
     $image_name = addslashes($_FILES['photo_upload']['name']);
    $date = date("d-m-Y");
    $image = addslashes(file_get_contents($_FILES['photo_upload']['tmp_name']));
    $image=compress($image,$image,75);
    $insert = "INSERT INTO photos (photo_image,date_uploaded,image_name) VALUES ('" .$image. "','" .$date. "','" .$image_name. "')";
    $result = mysql_query($insert) or die(mysql_error());
     if($result==1){
     $status='<div class="alert alert-success"><a href="#" class="close" data-dismiss="alert">&times;</a>
    <strong>Success!</strong> '.$image_name.' has been uploaded successfully.
    </div>';}
else{
$status='
    <div class="alert alert-danger">
        <a href="#" class="close" data-dismiss="alert">&times;</a>
        <strong>Error!</strong> Please attach an image to upload.
    </div>';}
    unset($_FILES);}
    else {
    $status='
    <div class="alert alert-danger">
    <a href="#" class="close" data-dismiss="alert">&times;</a>
    <strong>Error!</strong> Please attach an image to upload.
    </div>';
    }
}
?>

image_compressor.php

<?php 
function compress($source, $destination, $quality) {

$info = getimagesize($source);

if ($info['mime'] == 'image/jpeg') 
    $image = imagecreatefromjpeg($source);

elseif ($info['mime'] == 'image/gif') 
    $image = imagecreatefromgif($source);

elseif ($info['mime'] == 'image/png') 
    $image = imagecreatefrompng($source);

imagejpeg($image, $destination, $quality);

return $destination;
}

1 个答案:

答案 0 :(得分:0)

您可能希望查找具有新宽度和高度的PHP函数ImageCopyResampled(),而不是仅仅处理质量

相关问题