在上传时定义图像最大和最小宽度高度

时间:2012-04-05 17:35:56

标签: php image image-resizing image-uploading

我真的搜遍了所有网站,并尝试使用GD getimagesize功能,但没有希望 请我在上传时设置最小和最大图像宽度和高度。如果不相等则显示错误; 这是原始代码:

    <?php
$page = "user_editprofile_photo";
include "header.php";
$task = ( isset($_POST['task']) ? $_POST['task'] : NULL );
// CHECK FOR ADMIN ALLOWANCE OF PHOTO
if( !$user->level_info['level_photo_allow'] )
{
  header("Location: user_home.php");
  exit();
}
// SET ERROR VARIABLES
$is_error = 0;
// DELETE PHOTO
if( $task == "remove" )
{
  $user->user_photo_delete();
  $user->user_lastupdate();
  exit();
}
// UPLOAD PHOTO
if( $task == "upload" )
{
  $user->user_photo_upload("photo");
  $is_error = $user->is_error;
  if( !$is_error )
  {
    // SAVE LAST UPDATE DATE
    $user->user_lastupdate(); 
    // DETERMINE SIZE OF THUMBNAIL TO SHOW IN ACTION
    $photo_width = $misc->photo_size($user->user_photo(), "100", "100", "w");
    $photo_height = $misc->photo_size($user->user_photo(), "100", "100", "h");

    // INSERT ACTION
    $action_media = Array(Array('media_link'=>$url->url_create('profile', $user->user_info['user_username']), 'media_path'=>$user->user_photo(), 'media_width'=>$photo_width, 'media_height'=>$photo_height));
    $actions->actions_add($user, "editphoto", Array($user->user_info['user_username'], $user->user_displayname), $action_media, 999999999, TRUE, "user", $user->user_info['user_id'], $user->user_info['user_privacy']);
  }
}

// GET TABS TO DISPLAY ON TOP MENU
$field = new se_field("profile", $user->profile_info);
$field->cat_list(0, 0, 0, "profilecat_id='{$user->user_info['user_profilecat_id']}'");
$cat_array = $field->subcats;


// ASSIGN VARIABLES AND INCLUDE FOOTER
$smarty->assign('is_error', $is_error);
$smarty->assign('cats', $cat_array);
include "footer.php";
?>

这里我添加了:

if( $task == "upload" )
$photo=$user->user_photo();
{
list($img_width, $img_height, $img_type, $attrs)=getimagesize($photo);
  if($img_width >= 193 && $img_height >= 290)
  $user->user_photo_upload("photo");
  $is_error = $user->is_error;
  if( !$is_error )

1 - 以这种方式:

if($task == "upload")$photo = $user->user_photo();
  { $user->user_photo_upload("photo");

根本不会上传图片.....

2-以这种方式:

    if($task == "upload"){  
$user->user_photo_upload("photo");
$photo = $user->user_photo();

没有效果..它甚至上传了16px图像。 谢谢 如果你能提供帮助,我将非常感激。

1 个答案:

答案 0 :(得分:2)

if($task == "upload")看起来并不完全包含您想要的内容。如果您以更易读的格式查看代码:

<?php 
$task = (isset($_POST['task']) ? $_POST['task'] : NULL);
$is_error = 0;
if($task == "remove")
{
    $user->user_photo_delete();
    $user->user_lastupdate();
    exit();

}
if($task == "upload") $photo = $user->user_photo();
{
    list($img_width, $img_height, $img_type, $attrs) = getimagesize($photo);
    if($img_width >= 193 && $img_height >= 290) $user->user_photo_upload("photo");
    $is_error = $user->is_error;
    if(!$is_error)
    {
        $user->user_lastupdate();
        $photo_width = $misc->photo_size($user->user_photo(), "100", "100", "w");
        $photo_height = $misc->photo_size($user->user_photo(), "100", "100", "h");
        $action_media = array(array('media_link' => $url->url_create('profile', $user->user_info['user_username']), 'media_path' => $user->user_photo(), 'media_width' => $photo_width, 'media_height' => $photo_height));
        $actions->actions_add($user, "editphoto", array($user->user_info['user_username'], $user->user_displayname), $action_media, 999999999, TRUE, "user", $user->user_info['user_id'], $user->user_info['user_privacy']);

    }

}
$field = new se_field("profile", $user->profile_info);
$field->cat_list(0, 0, 0, "profilecat_id='{$user->user_info['user_profilecat_id']}'");
$cat_array = $field->subcats;
$smarty->assign('is_error', $is_error);
$smarty->assign('cats', $cat_array);
include "footer.php";
?>

你可以看到$photo = $user->user_photo();可能应该是花括号。它总是会每次都以list()开始执行代码。

我不知道这是否是问题,但你似乎有正确的想法。我不知道$photo = $user->user_photo();返回什么,但文件存储在$_FILES['image']['tmp_name']中(如果您的输入名称有类型文件和名称图像),您可以使用{{1}获取文件大小信息}如http://www.php.net/manual/en/function.move-uploaded-file.php

所述

确保您的表单标记中包含getimagesize($_FILES['image']['tmp_name']),否则enctype="multipart/form-data"数组将为空。