gpt4 book ai didi

为不同的 mime 类型调整 PHP 图像大小

转载 作者:行者123 更新时间:2023-12-04 05:33:03 28 4
gpt4 key购买 nike

我是一个 php noob 并试图创建一个图像大小调整脚本,将上传的图像缩小到更小的尺寸。使用下面的当前脚本,我可以显示原始图像。新的宽度和高度似乎没有注册。任何帮助表示赞赏。

    <?php

if ($_SERVER['REQUEST_METHOD']=='POST') {
if (isset($_FILES['image']['tmp_name'])) {
if ($_FILES['image']['error']==0){
$new_dir = '/PHP_MySQL_Practice/uploaded/images';
$fullpath = $_SERVER['DOCUMENT_ROOT'].$new_dir;
if(!is_dir($fullpath)) {
mkdir($fullpath, 0777, TRUE);
}
//get file name and type(extension)
$name = $_FILES['image']['name'];
$type = $_FILES['image']['type'];

//separate image name after first "dot"
$separated = explode(".", $name);
//get string before the dot which was stored as the 1st item in the separated array.
$first = $separated[0];
//use php function pathinfo to get extension of image file
$ext = pathinfo($name, PATHINFO_EXTENSION);
//concatenate current timestamp (to avoid file overwrites) with the extracted file 'firstname' and add the extension
$name = time().'_'.$first.'.'.$ext;
$target = $fullpath.'/'.$name;

if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {

resizeImage($target, $name);
echo '<img src="' . $target . '"/>';

}

}
else {

echo 'there was an error in saving ur image file.';
}
}
else {

echo 'ur image could not be uploaded.';
}
}
else {
?>
<form method="POST" enctype="multipart/form-data">
<label>upload image: </label>
<input type="file" name="image" />
<input type="submit" />
</form>
<?php
}

//function to resize image
function resizeImage($dir, $img) {
list($src_w, $src_h) = getimagesize($dir);
$max_w = 150;
$max_h = 150;
if ($src_w > $max_w) {
$ratio = $max_w / $src_w;
$new_img_w = $src_w * $ratio;
}
else if ($src_h > $max_h) {
$ratio = $max_h / $src_h;
$new_img_h = $src_h * $ratio;
}
$img_mime_type = getimagesize($dir);
switch ($img_mime_type['mime']) {
case 'image/jpeg':
case 'image/pjpeg':
$src = imagecreatefromjpeg($dir);
return $src;
break;
case 'image/gif':
$src = imagecreatefromgif($dir);
return $src;
break;
case 'image/png':
$src = imagecreatefrompng($dir);
return $src;
break;
default:
return FALSE;
break;
}
$new_img = imagecreatetruecolor($new_img_w, $new_img_h);
imagecopyresampled($new_img, $src, 0, 0, 0, 0, $new_img_w, $new_img_h, $max_w, $max_h);
switch ($img_mime_type['mime']) {
case 'image/jpeg':
case 'image/pjpeg':
imagejpeg($new_img, $dir, 100);
break;
case 'image/gif':
imagegif($new_img, $dir);
break;
case 'image/png':
imagepng($new_img, $dir);
break;
default:
return FALSE;
break;
}

imagedestroy($src);
imagedestroy($new_img);
}
?>

最佳答案

那里有一些缺陷

  • imagegif只有 2 个参数 $im并保存位置
  • imagepng压缩级别从 0 到 9 而不是 100
  • 您没有保存图像,null来自 imagejpeg($new_img, null, 100);应该用 $dir 更改
  • 您最后的功能是 imagdestroy($src);错了,应该是imagedestroy($src);

  • 您的 switch应该是:
    switch ($img_mime_type['mime']) {
    case 'image/jpeg':
    case 'image/pjpeg':
    imagejpeg($new_img, $dir, 100);
    break;
    case 'image/gif':
    imagegif($new_img, $dir);
    break;
    case 'image/png':
    imagepng($new_img, $dir);
    break;
    default:
    return FALSE;
    break;
    }

    您的调整大小功能:
    function resizeImage($dir, $img) {
    //echo $dir; die;
    list($src_w, $src_h) = getimagesize($dir);
    $max_w = 150;
    $max_h = 150;
    if ($src_w > $max_w) {
    $ratio = $max_w / $src_w;
    $max_w = $src_w * $ratio;
    }
    else if ($src_h > $max_h) {
    $ratio = $max_h / $src_h;
    $max_h = $src_h * $ratio;
    }
    $img_mime_type = getimagesize($dir);
    $src = imagecreatefromstring(file_get_contents($dir));
    $new_img = imagecreatetruecolor($max_w, $max_h);
    imagecopyresampled($new_img, $src, 0, 0, 0, 0, $max_w, $max_h, $src_w, $src_h);
    switch ($img_mime_type['mime']) {
    case 'image/jpeg':
    case 'image/pjpeg':
    imagejpeg($new_img, $dir, 100);
    break;
    case 'image/gif':
    imagegif($new_img, $dir);
    break;
    case 'image/png':
    imagepng($new_img, $dir);
    break;
    default:
    return FALSE;
    break;
    }

    imagedestroy($src);
    imagedestroy($new_img);
    }

    关于为不同的 mime 类型调整 PHP 图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12345584/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com