2014-02-13 77 views
0

此代碼不會在任何地方發佈任何內容。它不是我的上傳腳本,它是從

http://salman-w.blogspot.com/2008/10/resize-images-using-phpgd-library.html

採取它的工作原理,只要細如我把表單行動領域的外部文件,裏面坐了這兩個功能。但是,爲什麼不能以自己的職位工作呢?

<form action="" method="post" encytype="multipart/form-data"> 
    Upload an image for processing<br> 
    <input type="file" name="Image1"><br> 
    <input type="submit" value="Upload"> 
</form> 


<?php 

if (isset($_POST['submit'])) { 

$result = process_image_upload('Image1'); 
if ($result === false) { 
    echo '<br>An error occurred while processing upload'; 
} else { 
    echo '<br>Uploaded image saved as ' . $result[0]; 
    echo '<br>Thumbnail image saved as ' . $result[1]; 
} 
} 

/* 
* PHP function to resize an image maintaining aspect ratio 
* http://salman-w.blogspot.com/2008/10/resize-images-using-phpgd-library.html 
* 
* Creates a resized (e.g. thumbnail, small, medium, large) 
* version of an image file and saves it as another file 
*/ 

define('THUMBNAIL_IMAGE_MAX_WIDTH', 150); 
define('THUMBNAIL_IMAGE_MAX_HEIGHT', 150); 

function generate_image_thumbnail($source_image_path, $thumbnail_image_path) 
{ 
    list($source_image_width, $source_image_height, $source_image_type) = getimagesize($source_image_path); 
    switch ($source_image_type) { 
     case IMAGETYPE_GIF: 
      $source_gd_image = imagecreatefromgif($source_image_path); 
      break; 
     case IMAGETYPE_JPEG: 
      $source_gd_image = imagecreatefromjpeg($source_image_path); 
      break; 
     case IMAGETYPE_PNG: 
      $source_gd_image = imagecreatefrompng($source_image_path); 
      break; 
    } 
    if ($source_gd_image === false) { 
     return false; 
    } 
    $source_aspect_ratio = $source_image_width/$source_image_height; 
    $thumbnail_aspect_ratio = THUMBNAIL_IMAGE_MAX_WIDTH/THUMBNAIL_IMAGE_MAX_HEIGHT; 
    if ($source_image_width <= THUMBNAIL_IMAGE_MAX_WIDTH && $source_image_height <= THUMBNAIL_IMAGE_MAX_HEIGHT) { 
     $thumbnail_image_width = $source_image_width; 
     $thumbnail_image_height = $source_image_height; 
    } elseif ($thumbnail_aspect_ratio > $source_aspect_ratio) { 
     $thumbnail_image_width = (int) (THUMBNAIL_IMAGE_MAX_HEIGHT * $source_aspect_ratio); 
     $thumbnail_image_height = THUMBNAIL_IMAGE_MAX_HEIGHT; 
    } else { 
     $thumbnail_image_width = THUMBNAIL_IMAGE_MAX_WIDTH; 
     $thumbnail_image_height = (int) (THUMBNAIL_IMAGE_MAX_WIDTH/$source_aspect_ratio); 
    } 
    $thumbnail_gd_image = imagecreatetruecolor($thumbnail_image_width, $thumbnail_image_height); 
    imagecopyresampled($thumbnail_gd_image, $source_gd_image, 0, 0, 0, 0, $thumbnail_image_width, $thumbnail_image_height, $source_image_width, $source_image_height); 
    imagejpeg($thumbnail_gd_image, $thumbnail_image_path, 90); 
    imagedestroy($source_gd_image); 
    imagedestroy($thumbnail_gd_image); 
    return true; 
} 

/* 
* Uploaded file processing function 
*/ 

define('UPLOADED_IMAGE_DESTINATION', './images/'); 
define('THUMBNAIL_IMAGE_DESTINATION', './thumbnails/'); 

function process_image_upload($field) 
{ 
    $temp_image_path = $_FILES[$field]['tmp_name']; 
    $temp_image_name = $_FILES[$field]['name']; 
    list(, , $temp_image_type) = getimagesize($temp_image_path); 
    if ($temp_image_type === NULL) { 
     return false; 
    } 
    switch ($temp_image_type) { 
     case IMAGETYPE_GIF: 
      break; 
     case IMAGETYPE_JPEG: 
      break; 
     case IMAGETYPE_PNG: 
      break; 
     default: 
      return false; 
    } 
    $uploaded_image_path = UPLOADED_IMAGE_DESTINATION . $temp_image_name; 
    move_uploaded_file($temp_image_path, $uploaded_image_path); 
    $thumbnail_image_path = THUMBNAIL_IMAGE_DESTINATION . preg_replace('{\\.[^\\.]+$}', '.jpg', $temp_image_name); 
    $result = generate_image_thumbnail($uploaded_image_path, $thumbnail_image_path); 
    return $result ? array($uploaded_image_path, $thumbnail_image_path) : false; 
} 
+0

你什麼錯誤? –

+0

我認爲你的問題可能是'form'標記中的'action =「」'。我發現一些瀏覽器不喜歡那樣。 'action =「?」'對於自我發佈更可靠。另外,'process_image_upload()'函數沒有錯誤檢查。 – staticsan

+0

我的調試日誌中沒有顯示錯誤,它只是刷新頁面。 –

回答

1

這不起作用,因爲沒有$_POST["submit"]定義。這意味着腳本沒有得到過去if (isset($_POST['submit']))

改變這一行:

<input type="submit" value="Upload"> 

這樣:

<input type="submit" name="submit" value="Upload"> 
+0

嗯,我改變了你說的,現在它的射擊「注意:未定義的索引:Image1 on line ...」,行是$ temp_image_path = $ _FILES [$ field] ['tmp_name']; $ temp_image_name = $ _FILES [$ field] ['name']; –

+0

這意味着$ _FILES ['Image1']不存在,這可能由許多因素引起。您的post_max_size ini設置可能會低於您上傳的圖像。 – Svenskunganka