Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => Images

image resizing.....without adding width property

images php coding issues or problems here.

Moderators: macek, egami, gesf

image resizing.....without adding width property

Postby chaitanya » Wed Apr 13, 2011 1:08 am

hi,all i have a problem...i am uploading an image...it successfully uploading...but my requirement is whenever i want to upload a large image it will resize and then only it has to be upload...my code is...


<?php
ob_start(); session_start();
include ("includes/common.php");
include("includes/authentication.php");
include("includes/dbconfig.php");
include("SimpleImage.php");
$module = "profile"; //IMP don't change
$id=$_SESSION['userid'];
//echo "SELECT * FROM users where id='$id'";
if($_POST[submit] == "save")
{

$fullname=$_POST['fullname'];
$email=$_POST['email'];
$location=$_POST['location'];
$path = "profile_image/" . $_FILES["file"]["name"];


$error=0;
if($_POST['fullname'] == "")
{
$fullname_error="name cannot be left blank";
$error = 1;
}
if($_POST['email'] == "")
{
$email_error="email cannot be left blank";
$error = 1;
}
$pattern = "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$";

if (eregi($pattern, $email)==false)
{
$email_error="enter a valid email adress";
$error = 1;
}
if($_POST['location'] == "")
{
$location_error="location cannot be left blank";
$error = 1;
}




if($error==0)
{

$result=mysql_query("SELECT id FROM users WHERE email='$email' AND id!=".$_SESSION['userid']);
$num_rows=mysql_num_rows($result);
if($num_rows > 0)
{
$error = 2;
/* $row = mysql_fetch_row($result);
$id_2=$row['id'];
if($id == $id_2)
{

}

else
{
echo $email_error="already exits";
$error = 1;
}*/

}
else

{

$result1=mysql_query("UPDATE users
SET full_name='$_POST[fullname]', email='$_POST[email]',location='$_POST[location]' WHERE id=$id");


/*echo "<pre>";
print_r($_FILES["file"]["size"]); */


if ( (is_uploaded_file($_FILES['file']['tmp_name'])) &&(($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/png")) && ($_FILES["file"]["size"] < 20000))
{
if(move_uploaded_file($_FILES["file"]["tmp_name"], "profile_image/" . $_FILES["file"]["name"]))
{
//echo GetImageSize("profile_image/" . $_FILES["file"]["name"]);
/*$tmpimg = tempnam("/tmp", "MKUP");
system("djpeg $imgfile >$path");
system("pnmscale -xy 250 200 $tmpimg | cjpeg -smoo 10 -qual 50 >$path");
unlink($tmpimg);*/

$result1=mysql_query("UPDATE users SET picture='$path' WHERE id=$id");


}


}
else if($_FILES["file"]["tmp_name"]!='')
{
echo $error=3;
}

//echo "saved successfully";//header("location:contactimage1.php");


}
//echo "saved successfully";//header("location:contactimage1.php");


}

}//end of if



$result = mysql_query("SELECT * FROM users where id='$id'");
while($row = mysql_fetch_array($result))
{
$fullname= $row['full_name'];
$email = $row['email'];
$location = $row['location'];
$imgpath = $row['picture'];

}

?>


this is my code the problem is at only uploading image...pls check my code once...and how i will resize my uploaded image and how it will dispaly......in my code fileuploading is workng..pls explain resizing of A IMAGE.....
chaitanya
New php-forum User
New php-forum User
 
Posts: 25
Joined: Fri Mar 18, 2011 3:18 am

Re: image resizing.....without adding width property

Postby annyphp » Wed Apr 20, 2011 6:09 pm

Hi,
Maybe you can use the follow scripts:
Code: Select all
<?PHP
/*  Class Created by Anirban Nath (India) email :hide@address.com (software programmer , Kaizen tech, Chennai(India) ,expert in ZEND,JOOMLA(CMS+LMS) *
       
      This class basically has two function ie; (1) checkfile and (2) fileoperation..*
      while the former is for secure image uploading and the latter for resizing the image and storing it in Database*
      whether you can use this code, yes obiously ...but you have to retain the copyright code and credit for author!! *
      last but not least , iwould like to thank all the authors of this sites , for thier valuable works and code...that inspired me *
      ENJOY FOLKS!!! HOPE YOU LIKE IT... HAPPY CODING */
 /*for DB connection here iam not using any classes to make it simple clean, you can use DB connection*/

class secureimage
{
      var $open ;
      var $content;
      var $im;
      var $width;
      var $height;
      var $instr ;
      var $thumb;
      var $imgw;
      var $imgwh;
      var $xscale;
      var $yscale;
      var $new_width;
      var $new_height;
      var $msg="sorry,  image size exceeds!";
      var $msg2="sorry,  format not allowed!";
         var $redsize;
      
   function checkfile( $size,$error, $name) {  /* this function is for secure image uploading, ie invalid and malicious files uploading is restricted */
                     
                        if ( $error>0){
                        echo "ERROR:". $error;}
                        else{
                              
                                      if($size>200005000){ /* max size , you can check this according to your needs */
                                                                           echo $this->msg;
                                                                            return false;}
                     
                     elseif (exif_imagetype($name) != IMAGETYPE_GIF && exif_imagetype($name) != IMAGETYPE_JPEG &&
                     exif_imagetype($name) != IMAGETYPE_PNG )                                                                                                            /* CHECK exif_imagetype() manual in php.net */
                     { echo $this->msg2;
                        return false;}
                     
                     else {return true;} /* returns true , if image is secured ,,,then the execution of function fileoperation() is executed*/       

                                                            }   }



function fileoperation ($tmpname){     /* note this function is execueted if and only function checkfile() return true..this function is for uploading image(reaized) into mysql db.... */


$this->open = fopen($tmpname, 'r+');
$this->content = fread($this->open, filesize($tmpname));


fclose($this->open);

 $this->im = imagecreatefromstring($this->content);
                              
                                     $this->width = imagesx($this->im); /* get the width of the image*/
                                         $this->height = imagesy($this->im);  /*get the height of the image*/         
                                                                 
                                     $this->xscale=$this->width/150;  /* this is the new size 150 px width, you can change this to your own requirements*/
                                      $this->yscale=$this->height/180;        /* this is the new size 180 px height, you can change this to your own requirements*/
                              
                              if ($this->yscale>$this->xscale){
        $this->new_width = round($this->width * (1/$this->yscale)); /* new width*/
        $this->new_height = round($this->height * (1/$this->yscale)); /* new height */
    }
    else {
        $this->new_width = round($this->width * (1/$this->xscale));
        $this->new_height = round($this->height * (1/$this->xscale));
    }
                                                          
                                         # create new image using thumbnail-size
                                         $this->thumb=imagecreatetruecolor( $this->new_width, $this->new_height);                 
                                         # copy original image to thumbnail
                                        imagecopyresampled($this->thumb,$this->im,0,0,0,0,$this->new_width,$this->new_height, $this->width , $this->height); #makes thumb
                                         
                               imagejpeg($this->thumb, "my.jpg", 85);           
                                         $this->redsize=filesize("my.jpg");   /* getting the reduced size */
                               $this->instr = fopen("my.jpg","r+"); #need to move this to a safe directory
                                     $this->image = addslashes(fread($this->instr,filesize("my.jpg")));
                            
                            return  $this->image;
}

}
?>

Need some free PHP Image handling scripts which includes PHP image resizing and upload image.
annyphp
New php-forum User
New php-forum User
 
Posts: 76
Joined: Sun Nov 28, 2010 6:33 pm

Re: image resizing.....without adding width property

Postby jspmahavir » Thu Apr 21, 2011 2:43 am

hello macek

For resize image u must set single parameter to the function is width/height based on that we calculate the actual image size of the given image. and we just re size original image.
User avatar
jspmahavir
New php-forum User
New php-forum User
 
Posts: 14
Joined: Wed Apr 20, 2011 4:53 am
Location: Ahmedabad, Gujarat, India.

Re: image resizing.....without adding width property

Postby webinfopedia » Thu May 26, 2011 5:08 am

Use the dynamic thumbnail creation system. No need to re size the image. It will re size automatically and also will reduce the file size to load quickly.
Find the Dynamic thumbnail creation method below
http://www.webinfopedia.com/create-thumbnail-image-dynamically-using-PHP.html

Hope it will help you
Thank You
User avatar
webinfopedia
New php-forum User
New php-forum User
 
Posts: 84
Joined: Sat May 21, 2011 10:26 pm


Return to PHP coding => Images

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron