need help resizing images

This is a discussion on "need help resizing images" within the PHP Forum section. This forum, and the thread "need help resizing images are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Jul 15th, 2007, 04:35
New Member
Join Date: Jul 2007
Location: Chile!
Age: 26
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
need help resizing images

ok i have this php script and i need to resize images upon upload.. it has to be max width 350px and max height the same. can anyone help me with this?


heres the upload code of the script:
PHP: Select all

<?php
//session_start();
include("./admin/config.php");
include(
"$include_path/common.php");

global 
$HTTP_POST_VARS,$HTTP_GET_VARS,$HTTP_SESSION_VARS,$HTTP_POST_FILES;
global 
$_SESSION;

if (
$HTTP_POST_VARS!="")
    
$_POST=$HTTP_POST_VARS;

if (
$HTTP_GET_VARS!="")
    
$_GET=$HTTP_GET_VARS;

if (
$HTTP_SESSION_VARS!="")
   
$_SESSION=$HTTP_SESSION_VARS;

if (
$HTTP_POST_FILES!="")
   
$_FILES=$HTTP_POST_FILES;


check_user_login();

$img_src "";

include(
"$include_path/$table_file");

if(isset(
$_POST['delete_image']))
{
    
    
$sql "
        select
            concat(id, '.', image_ext) as image
        from
            $tb_users
        where
            id = '$_SESSION[userid]'
    "
;
    
$query mysql_query($sql) or die(mysql_error());
    
    
$file $image_path "/" mysql_result($query0"image");
    
    if(
file_exists($file))
    {
        
        if(
unlink($file)){
            
drop_rotation($_SESSION['userid']);
            
set_notfound_image($_SESSION['userid']);
            
$message "Tu foto ha sido borrada.";
        } else {
            
$message "Ha ocurrido un error, tu foto no se ha podido borrar.";
        }

    } else {
        
$message "No se encontro ninguna foto.";
    }
}

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

    
$sql "
                select
                    total_files
                from
                    $tb_users
                where
                    id = '$_SESSION[userid]'
    "
;
    
$query mysql_query($sql) or die(mysql_error());
    
$total mysql_result($query0"total_files");

    if (
$total <= 20
    {

        
$sql "
            select
                image_status
            from
                $tb_users
            where
                id = '$_SESSION[userid]'
        "
;
        
$query mysql_query($sql) or die(mysql_error());
    
        
$res mysql_fetch_array($query);
        if (
$res['image_status']!="disabled")
        {

            
$upload_image_error false;
            
$allowed false;

            
$the_file_ext_array explode("."$_FILES['the_file']['name']);
            
$the_file_ext $the_file_ext_array[sizeof($the_file_ext_array)-1];

            if(!
strlen($_FILES['the_file']['name'])) 
            {
            
                
$upload_image_error true;
                
$message "No subiste nada!<br><br>";
        
            }

            
$sql "
                select
                *
                from
                $tb_image_types
            "
;
            
$query mysql_query($sql) or die(mysql_error());
    
            while(
$array mysql_fetch_array($query))
                if(
$the_file_ext == $array["ext"]) $allowed true;
    
            if(!
$allowed)
            {

                
$upload_image_error true;
            
                
$message "El tipo de archivo no es permitido<br>solo puedes subir:<br><br>";

                
$ssql "
                    select
                        *
                    from
                        $tb_image_types
                "
;
                
$squery mysql_query($ssql) or die(mysql_error());
            
                while(
$sarray mysql_fetch_array($squery))
                    
$message .= "." $sarray["ext"] . "<br>";

            }
    
            if(
$allowed)
            {
            
            
// echo " tmp ".$_FILES['the_file']['tmp_name'];

                
$size getimagesize($_FILES['the_file']['tmp_name']);
                list(
$foo$width$bar$height) = explode("\""$size[3]);
        
                if(
$width $max_image_width)
                {
            
                    
$upload_image_error true;
                    
                    
$message "Tu imagen no puede ser mas de " $max_image_width " píxeles de ancho<br><br>";
                }
        
                if(
$height $max_image_height)
                {
    
                    
$upload_image_error true;

                    
$message "Tu imagen no puede ser mas de " $max_image_height " píxeles de alto<br><br>";
                }
    
            }

            if(!
$upload_image_error)
            {
                
$sql "
                    select
                        total_files
                    from
                        $tb_users
                    where
                        id = '$_SESSION[userid]'
                "
;
                
$query mysql_query($sql) or die(mysql_error());
                
$total mysql_result($query0"total_files");
                
$number $total 1;
                
$main_picture$_POST['pic_type'];

                if(
$total && $main_picture=="y") {
                    
$number=1;
                }                
                
$file_name $_SESSION['userid'] . "_$number." $the_file_ext;

                if(isset(
$_POST['overwrite']))
                {
                    if(
move_uploaded_file($_FILES['the_file']['tmp_name'], $image_path "/" $file_name)) 
                    {
                        if(
$main_picture=="y") {
                                                
update_ext($the_file_ext$_SESSION['userid']);
                        }
                        
$newest_sql ="select * from $tb_newest_pictures where user_id ='$_SESSION[userid]' and image_number ='$number' ";
                        
$newest_res mysql_query($newest_sql);
                        if(
mysql_num_rows($newest_res) ) {
                            
$newest_picture_sql ="update $tb_newest_pictures set upload_time=now() where user_id ='$_SESSION[userid]' and image_number ='$number' ";
                            
mysql_query($newest_picture_sql);
                            echo 
mysql_error();    
                        } 
                        else {
                            
$newest_picture_sql ="insert into  $tb_newest_pictures(user_id,image_number,upload_time) values ('$_SESSION[userid]','$number',now() ) ";
                            
mysql_query($newest_picture_sql);
                            echo 
mysql_error();    
                        }
                if (
$automatically_approve_picture=="no"){
                        
$message "Tu foto se subio eitosamente. Porfavor espera 24 horas hasta que sea revisada."; }
                else {
                        
$message "Tu imagen fue subida exitosamente."; }

                                            
$uid=$_SESSION['userid'];
                                                    
$main=$_POST['pic_type'];
        
                                                if(
$main == "n")
                                                    
$desc=$_POST['pic_desc'];
                                                    
$sql="insert into $tb_upload_image_details (id,user_id,total_file_count,main,description)
                                         values('',$uid,$number,'$main','$desc')"
;

                                                
mysql_query($sql);
                        if(
$main == "n") {    
                            
$sql "update $tb_users set total_files=total_files+1 where id = '$_SESSION[userid]' ";
                            
$query mysql_query($sql) or die(mysql_error());
                        }
                        else {
                             if(!
$total) {    
                            
$sql "update $tb_users set total_files=total_files+1 where id = '$_SESSION[userid]' ";
                            
$query mysql_query($sql) or die(mysql_error());
                             }    
                        }

                    } 
                    else 
                    {
    
                        
$message "Tu imagen no se pudo subir, ocurrió un error de escritura.";    
                    }
    

                    
update_url("here"""$_SESSION['userid']);

                    if (
$automatically_approve_picture=="yes")
                            
approve_image($_SESSION['userid']);
                    else 
                            
queue_image($_SESSION['userid']);

                    
$imgsrc="$dat_base_url/images/$file_name";

                    
NewImageAddedNotification($_SESSION['userid'], $imgsrc);
                }
                else 
                {
            
                    if (!@
copy($_FILES['the_file']['tmp_name'], $image_path "/" $file_name))
                    {
                
                        
$message "Tu nueva foto no se pudo subir.";    
            
                    } 
                    else 
                    {
                
                        
$message "Tu nueva imagen fue subida exitosamente.";
                        
update_ext($the_file_ext$_SESSION['userid']);
            
                    }
            
                    
update_url("here"""$_SESSION['userid']);
            
                    if (
$automatically_approve_picture=="yes")
                        
approve_image($_SESSION['userid']);
                    else
                        
queue_image($_SESSION['userid']);

                    
$imgsrc="$dat_base_url/images/$file_name";

                    
NewImageAddedNotification($_SESSION['userid']);

                }
            } 
// if (uploade_image_error)

        
// if (disabled)
        
else
        {
            
$message "Tu foto fue desabilitada por el administrador.";
        }
    }
    else
    {
        
$message "Exediste el limite para subir imagenes...";
    }
}

if(isset(
$_POST['submit_image_url'])){
    
$message del_image($_SESSION[userid]);
    
$message update_url($_POST['update_where'], $_POST['new_image_url'], $_SESSION['userid']);

}

include(
"$include_path/doc_head.php");
include(
"$include_path/styles.php");


include (
"Ads_new.php");
include(
"$include_path/right.php");
include(
"$include_path/left.php");


// start content


$form "";
$location "";

if(isset(
$message)){
$message_code = <<<EOF
<table cellpadding="5" cellspacing="5" border="0">
<tr>
<td class="s9">$message</td>
</tr>
</table>
EOF;
}

$user_id = isset($_SESSION['userid']) ? $_SESSION['userid'] : 0;

$image_src get_image($user_id,3);

include(
"$include_path/image_rules.php");
$image_code $image_rules;


$tpl->assign(array('IMAGE_SRC' => $image_src));
$tpl->parse('IMAGE_CODE''upload_image_code');
$image_code .= $tpl->fetch('IMAGE_CODE');


if(
$allow_local_image == 1){
  
$tpl->assign(array('MAX_IMAGE_SIZE' => $max_image_size));
  
  
$location query_where($user_id);
  
  if(
$location == "here" || !$allow_remote_image){
    
$tpl->assign(array('CHECKED_LOCAL_IMAGE' => ' checked'));
      
$img_src "";
  }
  
  
$tpl->parse('FIELD_LOCAL_IMAGES''field_local_image');
} else {
  
$tpl->clear_dynamic('field_local_image');
}

if(
$allow_remote_image == 1){
  if(
$location == "there" or $allow_local_image == 0) {
    
$tpl->assign(array('CHECKED_REMOTE_IMAGE' => " checked"));
  }
  
  
$the_img_src strstr($img_src"notfound_image") ? "" $img_src;
  
$tpl->assign(array('THE_IMG_SRC' => $the_img_src));
  
  
$tpl->parse('FIELD_REMOTE_IMAGE''field_remote_image');
} else {
  
$tpl->clear_dynamic('field_remote_image');
}



$tpl->parse('FORM''upload_form');
$form $tpl->fetch('FORM');

$title "Subir Foto";

if(isset(
$_SESSION['userid']))
    
$content .= $image_code;

if(isset(
$message_code)) $content .= $message_code;

if(isset(
$_SESSION['userid'])) $content .= $form;

$final_output table($title$content);


$tpl->assign(array('CONTENT_TEXT' => $final_output));
$tpl->parse('PAGE''main');
$final_output $tpl->fetch('PAGE');



$final_output final_output($final_output);

//echo $final_output;
include ("copy.php");

function 
NewImageAddedNotification($uid$img)
{
    global 
$tb_users$tb_user_types$dat_base_url$owner_email$tb_email_templates;

    
$sql "select u.username, ut.user_type from $tb_users u, $tb_user_types ut where u.id = '$uid' and u.user_type=ut.id ";
    
$res mysql_query($sql);
    echo 
mysql_error();

    if (
$data mysql_fetch_row($res))
    {
    
$username $data[0];
    
$usertype $data[1];
    }    
     
     
$mailheader="From: ".$owner_email;
     
$mailheader.="\r\nMIME-Version: 1.0\r\n";
     
$mailheader.="Content-type: text/html; charset=iso-8859-1\r\n";

     
$subject "Nueva imagen agregada en $dat_base_url ";

    
$sql "select  file_content  from $tb_email_templates  where file_name = 'new_image_notification' ";
    
$res mysql_query($sql);
    echo 
mysql_error();

    if (
$data mysql_fetch_row($res))
    {
    
$message $data[0];    
    }    

    
    
$message ereg_replace("<<BASE_URL>>",$dat_base_url$message);
    
$message ereg_replace("<<USERNAME>>",$username$message);
    
$message ereg_replace("<<USERTYPE>>",$usertype$message);
    
$message ereg_replace("<<IMAGE_SRC>>",$img$message);
    
    
$to $owner_email;
    
mail($to$subject$message$mailheader);        
}

?>
all help will be greatly appreciated
Reply With Quote

  #2 (permalink)  
Old Jul 15th, 2007, 12:39
Up'n'Coming Member
Join Date: Sep 2006
Location: UK
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
Re: need help resizing images

Quote:
PHP: Select all

global $HTTP_POST_VARS,$HTTP_GET_VARS,$HTTP_SESSION_VARS,$HTTP_POST_FILES;

global 
$_SESSION;

if (
$HTTP_POST_VARS!="")
    
$_POST=$HTTP_POST_VARS;

if (
$HTTP_GET_VARS!="")
    
$_GET=$HTTP_GET_VARS;

if (
$HTTP_SESSION_VARS!="")
   
$_SESSION=$HTTP_SESSION_VARS;

if (
$HTTP_POST_FILES!="")
   
$_FILES=$HTTP_POST_FILES
What version of PHP are you using?

After PHP 4.1.0, $_SESSION, $_POST, $_FILES, etc, are all superglobals.
I.e. They are predefined variables that are already global. $HTTP_*_VARS is deprecated; just use $_*.
$_SESSION is a superglobal, so you don't need to make it global - it automatically is.

On to the image sizes.
You have this in your code...does it work for you?
PHP: Select all

list($foo$width$bar$height) = explode("\""$size[3]); 

It seems that you are splitting the img tag string to find the width and height.
This would probably be better:
PHP: Select all

$width $size[0];

$height $size[1];

// or

list($width$height) = getimagesize($_FILES['the_file']['tmp_name']); 
Also, in $size[3], height comes before width, but in your list() width comes before height.

On to the resizing:
PHP: Select all

           if($allowed)

            {
            
                
$size getimagesize($_FILES['the_file']['tmp_name']);
                
$width $size[0];
                
// image width
                
$height $size[1];
                
// image height

                
if($width $max_image_size || $height $max_image_size) {
                
// since max_image_width and max_image_height are both 350, might as well just use one variable.

                     
if ($width $height) {
                     
// if the width is more than the height, resize image according to width proportions
                     
$ratio = ($max_image_size $width);
                     
// ratio to resize by == max size of image divided by width
                     

                     else {
                     
// if the height is more than the width, resize image according to height proportions
                     
$ratio = ($max_image_size $height);
                     }
               

                     
$newwidth round($width $ratio);
                     
// new width
                     
$newheight round($height $ratio);
                     
// new height

                     
$newimage imagecreatetruecolor($newwidth$newheight);
                     
// start making new image
                     
$source imagecreatefromjpeg($_FILES['the_file']['tmp_name']);
                     
// original image
 
                     
imagecopyresized($newimage$source0000$newwidth$newheight$width$height);
                     
// resize image

                     
imagejpeg($newimage); // or whatever image type it is
                     // make new image

                
}
             } 

Last edited by balaclave; Jul 15th, 2007 at 12:50.
Reply With Quote
  #3 (permalink)  
Old Jul 16th, 2007, 04:08
New Member
Join Date: Jul 2007
Location: Chile!
Age: 26
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: need help resizing images

ok, will try to implement that

originally the script resizes thye image dynamically ( i thnk thats the term) so the fotos take way too much time to load.... and its a hassle asking users to upload small photos since most of them dont have a clue how to do it :P


thank you very very much
Reply With Quote
Reply

Tags
help, image, img, resize

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads