Define image max and min width height on upload

Go To StackoverFlow.com

-2

I really searched all the site and tried to use GD getimagesize function,But no hope please i want to set min and max image width and height on upload..so if not equal then show error; here is original code:

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

here what i add:

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-In this way:

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

no image will uploaded at all.....

2- in this way:

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

No effect..and it uploads even 16px image. Thank you Please if you can help.i will be so grateful.

2012-04-05 17:35
by Sanshiro
You can try formatting that code, to help us help you - gosukiwi 2012-04-05 17:38
I tried but the editor..always give me error not match our rules - Sanshiro 2012-04-05 17:43


2

It looks like your if($task == "upload") isn't quite encompassing what you want it to. If you look at the code in a little more human-readable format:

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

You can see that the $photo = $user->user_photo(); should probably go instead the curly braces. It's always going to execute the code starting with the list() every time.

I don't know if that's the issue, but you seem to have the right idea. I don't know what $photo = $user->user_photo(); returns, but the file is stored in $_FILES['image']['tmp_name'] (if your input name has type file and name image) and you can get the filesize info with getimagesize($_FILES['image']['tmp_name']) as documented on http://www.php.net/manual/en/function.move-uploaded-file.php

Make sure your form tag has enctype="multipart/form-data" in it or the $_FILES array will be empty.

2012-04-05 17:55
by CWSpear
Ads