2014-10-18 104 views
0

我試圖在上傳到我的數據庫時縮小圖像的大小。但問題是,當我上傳大小時仍然保持不變。香港專業教育學院附我下面任何幫助代碼是感激accepted.thank你在上傳過程中減少php的圖像大小

<?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; 
} 

回答

0

你可能想尋找PHP函數ImageCopyResampled()一個新的高度和寬度,而不是對質量工作的只有