我需要在上传时调整图像的大小


I need to resize image on upload

你能帮我解决这个问题吗?我需要在上传时使用一些简单的脚本调整图像的大小。

代码:

<form method="post" enctype="multipart/form-data">
    <input type="file" name="obrazky[]" />
    <input type="submit" value="Nahrát na server!" />
</form>
<?php
// konfigurace
$uploadDir = 'imgs/nahrane'; // dir
$allowedExt = array('jpg', 'jpeg', 'png', 'gif'); // ok formats
if(isset($_FILES['obrazky']) && is_array($_FILES['obrazky']['name'])) {
    $counter = 0;
    $allowedExt = array_flip($allowedExt);
    foreach($_FILES['obrazky']['name'] as $klic => $nazev) { // make variable $nazev
        $nazev = htmlspecialchars($nazev, ENT_QUOTES);
        $fileName = basename(time() . ".png");
        $fileName = sprintf(time() . ".png", pathinfo($nazev, PATHINFO_EXTENSION));
        $tmpName = $_FILES['obrazky']['tmp_name'][$klic];
        $fileName = htmlspecialchars($fileName, ENT_QUOTES);
        // check
        if(
            !is_uploaded_file($tmpName)
            || !isset($allowedExt[strtolower(pathinfo($fileName, PATHINFO_EXTENSION))])
        ) {
            // bad format
            continue;
        }
        // move
        if(move_uploaded_file($tmpName, "{$uploadDir}".DIRECTORY_SEPARATOR."{$fileName}")) {
            ++$counter;
        }
  }
    if ($counter > 0)
    echo "<hr><p>Uploaded {$counter} z ".sizeof($_FILES['obrazky']['name'])." thank you!<br>
    <br>
    <b>LINKS</b>:<br>
    href:<br> 
    <a href='$uploadDir/$fileName'  target='_blank'>$uploadDir/$fileName</a><br>
 
    else
    echo "<hr><b><font color='red'>ERROR: Bad format or no image uploaded.</font></b><br>"; 
}
 
?>

我在google和stackoverflow上做了allready搜索,我尝试了allready something,但我没有得到它的工作

这对我来说一直很有效:

$image = new Imagick();
$image_filehandle = fopen('some/file.jpg', 'a+');
$image->readImageFile($image_filehandle );
$image->scaleImage(100,200,FALSE);
$image_icon_filehandle = fopen('some/file-icon.jpg', 'a+');
$image->writeImageFile($image_icon_filehandle);

您可能希望基于原始图像更动态地计算宽度和高度。使用上面的例子,你可以使用$image->getImageHeight()获取图像的当前宽度和高度;和图像-> getImageWidth();。