dongzhuji1042 2013-07-14 20:11
浏览 64

如何在上传到MySQL数据库之前减少/压缩图像的大小

I need to know what i would have to do with this code in order to reduce/compress the size of the image being uploaded if a the image being uploaded is over 1mb.

<form enctype="multipart/form-data" action='upcomg.php' method="post" name="changer">
<input name="MAX_FILE_SIZE" value="83886080" type="hidden">
<label>Please choose a file:</label><input name="image" accept="image/jpeg" type="file">
<input value="Submit" type="submit">

<?php
    if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) {

    $tmpName = $_FILES['image']['tmp_name'];

    $fp = fopen($tmpName, 'r');
    $data = fread($fp, filesize($tmpName));
    $data = addslashes($data);
    fclose($fp);


    $query = "INSERT INTO ### ";
    $query .= "(##) VALUES ('$data')";
    $results = mysql_query($query, $link);

    // Print results
    print "Thank you, your file has been uploaded.";

    }
    else {
    print "No image selected/uploaded";
    }

    // Close our MySQL Link
    mysql_close($link);
?> 
  • 写回答

1条回答 默认 最新

  • dongyong3554 2013-07-14 20:15
    关注

    First off, I wouldn't be storing raw image data in the DB. Storing it in a regular old static file will be much quicker and also won't break if your DB goes offline.

    But with that being said, you could just put something like this in there:

    $file = $_FILES['image']['tmp_name'];
    if ( $_FILES['image']['size'] > 1024*1024 )
        $file = compress($file);
    

    Do you have a method of compression already? If not I can help you with that too, but you might want to take a look at Imagick because it already has built in compression.

    评论

报告相同问题?

悬赏问题

  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线