如何调整图像大小并减小其文件大小?
这是我的用于减小图像文件大小的代码,该代码可以正常工作:
<!DOCTYPE html>
<html>
<?PHP
if(isset($_POST["submit"]))
{
include("connect.php");
// created compressed JPEG file from source file
function compressImage($source_image, $compress_image) {
$image_info = getimagesize($source_image);
if ($image_info['mime'] == 'image/jpeg') {
$source_image = imagecreatefromjpeg($source_image);
imagejpeg($source_image, $compress_image, 75);
} elseif ($image_info['mime'] == 'image/gif') {
$source_image = imagecreatefromgif($source_image);
imagegif($source_image, $compress_image, 75);
} elseif ($image_info['mime'] == 'image/png') {
$source_image = imagecreatefrompng($source_image);
imagepng($source_image, $compress_image, 6);
}
return $compress_image;
}
$upload_dir = "test/";
$new_image_name_rand_num = RAND(0,100);
$new_image_name_rand_text = substr(str_shuffle('ABCDEFGHIJKLMNOPQRSTUVWXYZ23456789'),0,7);
$image_name_new = $pet_ref_id."-".$new_image_name_rand_num."-".$new_image_name_rand_text;
$temp = explode(".", $_FILES['pet_image']['name']);
$newfilename = $image_name_new. '.' . end($temp);
$upload_file = $upload_dir.$newfilename;
if(move_uploaded_file($_FILES['pet_image']['tmp_name'],$upload_file))
{
$source_image = $upload_file;
$image_destination = $upload_dir."".$newfilename;
$compress_images = compressImage($source_image, $image_destination);
}
}
?>
<form class="form" method="post" action="" ENCTYPE = "multipart/form-data">
<input name="pet_image" type="file"/>
<input type="submit" name="submit" value="OK">
</form>
</body>
</html>
但是当我像这样减小文件大小并重新调整图像大小时,既不会减小文件大小也不会调整图像大小。同时做这两项的正确方法是什么?
这是无法正常运行的组合代码:
<!DOCTYPE html>
<html>
<?PHP
if(isset($_POST["submit"]))
{
include("connect.php");
// created compressed JPEG file from source file
function compressImage($source_image, $compress_image) {
$image_info = getimagesize($source_image);
if ($image_info['mime'] == 'image/jpeg') {
$source_image = imagecreatefromjpeg($source_image);
imagejpeg($source_image, $compress_image, 75);
} elseif ($image_info['mime'] == 'image/gif') {
$source_image = imagecreatefromgif($source_image);
imagegif($source_image, $compress_image, 75);
} elseif ($image_info['mime'] == 'image/png') {
$source_image = imagecreatefrompng($source_image);
imagepng($source_image, $compress_image, 6);
}
return $compress_image;
}
function shazam($file, $w, $h) {
list($width, $height) = getimagesize($file);
if ($width > $height) {
$r = ($w / $width);
$newwidth = $w;
$newheight = ceil($height * $r);
}
if ($width < $height) {
$r = ($h / $height);
$newheight = $h;
$newwidth = ceil($width * $r);
}
if ($width == $height) {
$newheight = $h;
$newwidth = $w;
}
$src = imagecreatefromjpeg($file);
$tgt = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($tgt, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
return $tgt;
}
$upload_dir = "test/";
$new_image_name_rand_num = RAND(0,100);
$new_image_name_rand_text = substr(str_shuffle('ABCDEFGHIJKLMNOPQRSTUVWXYZ23456789'),0,7);
$image_name_new = $pet_ref_id."-".$new_image_name_rand_num."-".$new_image_name_rand_text;
$temp = explode(".", $_FILES['pet_image']['name']);
$newfilename = $image_name_new. '.' . end($temp);
$upload_file = $upload_dir.$newfilename;
if(move_uploaded_file($_FILES['pet_image']['tmp_name'],$upload_file))
{
$source_image = shazam($upload_file, 850, 850);
$image_destination = $upload_dir."".$newfilename;
$compress_images = compressImage($source_image, $image_destination);
}
}
?>
<form class="form" method="post" action="" ENCTYPE = "multipart/form-data">
<input name="pet_image" type="file"/>
<input type="submit" name="submit" value="OK">
</form>
</body>
</html>