|
| | |
| 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: 591 |
Added by: admin-priyank
| Rating: 5.0/1 |
| |
| | |
|
Calendar |
| « November 2012 » | Su | Mo | Tu | We | Th | Fr | Sa | | | | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 |
| |
|
Our poll |
| | |
|
Statistics |
|
Total online: 2 Guests: 2 Users: 0 | |
|
|