2012-12-05 32 views
0

我正在與通過AJAX PHP腳本,其上載圖像的從this source.如何在此PHP腳本中調整圖像大小?

陣列雖然它的工作原理優良我試圖進入PHP代碼,其通過寬度或高度(取大)調整大小的圖像以120像素的實驗。 對於我的理智,請幫助如何更新代碼:

$demo_mode = false; 
$upload_dir = 'uploads/'; 
$allowed_ext = array('jpg','jpeg','png','gif'); 

if(strtolower($_SERVER['REQUEST_METHOD']) != 'post'){ 
    exit_status('Error! Wrong HTTP method!'); 
} 

if(array_key_exists('pic',$_FILES) && $_FILES['pic']['error'] == 0){ 

    $pic = $_FILES['pic']; 

    if(!in_array(get_extension($pic['name']),$allowed_ext)){ 
     exit_status('Only '.implode(',',$allowed_ext).' files are allowed!'); 
    } 

    if($demo_mode){ 

     // File uploads are ignored. We only log them. 

     $line = implode('  ', array(date('r'), $_SERVER['REMOTE_ADDR'], $pic['size'], $pic['name'])); 
     file_put_contents('log.txt', $line.PHP_EOL, FILE_APPEND); 

     exit_status('Uploads are ignored in demo mode.'); 
    } 

    // Move the uploaded file from the temporary 
    // directory to the uploads folder: 

    if(move_uploaded_file($pic['tmp_name'], $upload_dir.$pic['name'])){ 
     exit_status('File was uploaded successfuly!'); 
    } 

} 

exit_status('Something went wrong with your upload!'); 

// Helper functions 

function exit_status($str){ 
    echo json_encode(array('status'=>$str)); 
    exit; 
} 

function get_extension($file_name){ 
    $ext = explode('.', $file_name); 
    $ext = array_pop($ext); 
    return strtolower($ext); 
} 

回答

0

好了,我把「promaty」的一個例子,從這個LINK和循環撥打:

<?php 

$dir = "uploads/*.*"; 
$images = glob($dir); 

foreach($images as $file): 
    $px = 120; 
    $prefix = $px . "_"; 
    $dst = "uploads/" . $prefix . basename($file); 
    $resized = image_resize($file, $dst, $px, $px); 
    echo "<img src='" . $dst . "' />"; 
    echo "<p>" . basename($dst) . " </p>"; 
endforeach; 

...並感謝您的時間。

1

您可以在服務器上安裝imagemagick

<?php 
function resize_image($file, $w, $h, $crop=FALSE) { 
    $img = new Imagick($file); 
    if ($crop) { 
     $img->cropThumbnailImage($w, $h); 
    } else { 
     $img->thumbnailImage($w, $h, TRUE); 
    } 

    return $img; 
} 
resize_image(‘/path/to/some/image.jpg’, 150, 150); 

另一種方法是使用PHP-GD

<?php 
function resize_image($file, $w, $h, $crop=FALSE) { 
    list($width, $height) = getimagesize($file); 
    $r = $width/$height; 
    if ($crop) { 
     if ($width > $height) { 
      $width = ceil($width-($width*($r-$w/$h))); 
     } else { 
      $height = ceil($height-($height*($r-$w/$h))); 
     } 
     $newwidth = $w; 
     $newheight = $h; 
    } else { 
     if ($w/$h > $r) { 
      $newwidth = $h*$r; 
      $newheight = $h; 
     } else { 
      $newheight = $w/$r; 
      $newwidth = $w; 
     } 
    } 
    $src = imagecreatefromjpeg($file); 
    $dst = imagecreatetruecolor($newwidth, $newheight); 
    imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); 

    return $dst; 
} 

$img = resize_image(‘/path/to/some/image.jpg’, 150, 150); 
+0

Kuf,謝謝,但你給我的功能,我必須從循環調用收集所有圖像文件。 ?請參見:如果(array_key_exists( 'PIC',$ _ FILES)&& $ _FILES [ 'PIC'] [ '錯誤'] == 0){ \t $ PIC = $ _FILES [ 'PIC']; $ pic = resize_image('uploads /',150,150); – Alex

1
<?php 
// The file 
$filename = 'test.jpg'; 
$percent = 0.5; 

// Content type 
header('Content-Type: image/jpeg'); 

// Get new dimensions 
list($width, $height) = getimagesize($filename); 
$new_width = $width * $percent; 
$new_height = $height * $percent; 

// Resample 
$image_p = imagecreatetruecolor($new_width, $new_height); 
$image = imagecreatefromjpeg($filename); 
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); 

// Output 
imagejpeg($image_p, null, 100); 

查找更多關於php.net

+0

我看了那個教程,但是不能將它包含在我的腳本中。雖然,用php迭代有問題;在JavaScript中,我可以做任何我不會做的事情,但在PHP中無法確定。 – Alex