MiX1.TK
Site menu
Login form
Section categories
Web Development [20]
Learn about PHP & Web Development
Education [4]
Learn for Free
Knowledge & Tricks [0]
General Knowledge,computer Knowledge & tricks
Writing World [2]
A MiX World of SmS on Various Topics
Search
Main » 2012 » November » 6 » Image Slicing Code in PHP
10:22 PM
Image Slicing Code in PHP
First of all make a php file and a folder naed   img  for saving the sliced images after that in your created php file copy and paste this code this code will provide you option for cutting the images in multiple dimensions . and make sure that the img named folder have all privileges to store images . mean to say having 777 permissions

<html>

<head></head>
<body>

<form enctype="multipart/form-data" method="post">


<h3>Please select any jpg,png,gif image file for cropping </h3>

Select file:<input type="file" name="file" />
<br/>
select Size: <select name="dimension" id="dimension" >
<option value="">Select dimension</option>
<?php
for($k=1;$k<=20;$k++)
{
    $dim=$k*100;
?>
<option value=""></option>
<?php
}
?>
</select>
<br/>
<input type="submit" name="submit" value="crop image" />

</form>

<?php
if($_POST)
{   
    move_uploaded_file($_FILES['file']['tmp_name'],$_FILES['file']['name']);
    $image_up=$_FILES['file']['name'];
    $img_name_arr=explode('.',$image_up);
    $img_name=$img_name_arr[0];
    $img_type=$img_name_arr[1];
        list($src_width, $src_height, $type, $attr) = getimagesize($image_up);
       
        $dimension=$_REQUEST['dimension'];
        if($dimension =='')
        {
            die("please select dimension !");
        }
        else if($src_width < $dimension || $src_height < $dimension)
        {
            die("please select a image having dimesion greater than your selected size !");
        }
        $dest_width=$dimension;
        $dest_height=$dimension;
       
       
        switch ($type)
        {
          case IMAGETYPE_GIF  : $src = imagecreatefromgif($image_up);  break;
          case IMAGETYPE_JPEG : $src = imagecreatefromjpeg($image_up); break;
          case IMAGETYPE_PNG  : $src = imagecreatefrompng($image_up);  break;
          default : die("Unknown filetype");
        }


        $rounds_width=ceil($src_width/$dest_width);
        $rounds_height=ceil($src_height/$dest_height);

        $dest_y=0;
       
        $slice_ctr=1;
       
        echo '<h1>original image</h1>';
        echo '<img src="'.$image_up.'" />';
       
        echo "<h2>Croped images in $dimension X $dimension </h2><table cellspacing='0' cellpadding='0' border='2'>";
       
       
       
        
   
       
       
        for($i=0;$i<$rounds_height;$i++)
        {       
           
               
            echo '<tr>';
            $dest_x=0;
           
            for($j=0;$j<$rounds_width;$j++)
            {       
                $tmp_height=$dest_height;
                $tmp_width=$dest_width;
                   
                if($j == $rounds_width-1 &&  ($dest_x+$dimension) > $src_width )
                {
                    $tmp_width=$src_width-$dest_x;
                }
                if($i == $rounds_height-1 &&  ($dest_y+$dimension) > $src_height )
                {
                    $tmp_height=$src_height-$dest_y;
                }
               
                $dest = imagecreatetruecolor($tmp_width,$tmp_height);
                imagecopy($dest, $src,0,0, $dest_x,$dest_y,$tmp_width,$tmp_height);
               
               
                switch ($type)
                {
                  case IMAGETYPE_GIF  : imagegif($dest,'./img/'.$img_name.'_'.$slice_ctr.'.gif');      break;
                  case IMAGETYPE_JPEG : imagejpeg($dest,'./img/'.$img_name.'_'.$slice_ctr.'.jpg',100); break;
                  case IMAGETYPE_PNG  : imagepng($dest,'./img/'.$img_name.'_'.$slice_ctr.'.png', 9);   break;
                  default : die("Unknown filetype");
                }
               
               

                imagedestroy($dest);
                $dest_x=$dest_x+$dest_width;
               
               
                echo '<td><img src="img/'.$img_name.'_'.$slice_ctr.'.'.$img_type.'" /></td>';
               
         
               
                $slice_ctr++;
               
            }
            echo '</tr>';
            $dest_y=$dest_y+$dest_height;
           
        }
        echo '</table>';
 
}

?>


</body>

</html>
Category: Web Development | Views: 203 | Added by: admin-priyank | Rating: 5.0/1
Total comments: 0
Name *:
Email *:
Code *:
Calendar
«  November 2012  »
SuMoTuWeThFrSa
    123
45678910
11121314151617
18192021222324
252627282930
Entries archive
Our poll
Rate my site
Total of answers: 4
Site friends
  • Create a free website
  • Online Desktop
  • Free Online Games
  • Video Tutorials
  • All HTML Tags
  • Browser Kits
  • Statistics

    Total online: 1
    Guests: 1
    Users: 0
    Creative Commons License
    Free Downloads and Services by MiX1 is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License
    Copyright MyCorp © 2018