2016-10-29 69 views
1

你能幫我解決這個問題嗎? 我需要使用一些簡單的腳本在上傳時調整圖像大小。我需要在上傳時調整圖像大小

CODE:

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

我沒有媒體鏈接在谷歌搜索和計算器,我試過媒體鏈接的東西,但我不得到它的工作。

+0

使用這個類其簡單和強大[鏈接](https://www.verot.net/php_class_upload.htm) –

+0

看看這個: [http://stackoverflow.com/questions/18805497/php-調整圖像上上傳](http://stackoverflow.com/questions/18805497/php-resize-image-on-upload) –

回答

1

這一直工作很好地描述了我:

$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()獲得圖像的當前寬度和高度;和$ image-> getImageWidth();.