Skip to content

Instantly share code, notes, and snippets.

@winwu
Last active December 16, 2015 04:39
Show Gist options
  • Save winwu/95b8be3a78ab889f4898 to your computer and use it in GitHub Desktop.
Save winwu/95b8be3a78ab889f4898 to your computer and use it in GitHub Desktop.
this file is an practice file.
<?php include("link_db.php");?>
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>Crop Result</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<?php
/**Please refer :
*http://www.php.net/manual/en/function.imagecreatefromjpeg.php
and http://php.net/manual/en/function.imagesx.php
http://www.5idev.com/p-php_gd.shtml
*/
echo"Request method is: <span style='color:red'>".$_SERVER['REQUEST_METHOD']."</span><br>";
echo "The POST Imformation:<br>";
print_r($_POST);
echo "裁切圖片的w點為:".$_POST['x']."<br>";
echo "裁切圖片的y點為:".$_POST['y']."<br>";
echo "裁切圖片的寬度為:".$_POST['w']."<br>";
echo "裁切圖片的高度為:".$_POST['h']."<br>";
echo "原圖檔名為:".$_POST['img_original_filename']."<br>";
$img_original_src = $_POST['img_original_src'];
//取得GIF、JPEG、PNG圖形的大小
$size = getimagesize($img_original_src);
#echo $size["mime"]; //得知該圖的mine type
//判斷user上傳的檔案類型是...
switch($size["mime"]){
case "image/jpeg":
$im = imagecreatefromjpeg($img_original_src) or die('Error ><');; //jpeg file
break;
case "image/gif":
$im = imagecreatefromgif($img_original_src) or die('Error ><');; //gif file
break;
case "image/png":
$im = imagecreatefrompng($img_original_src) or die('Error ><');; //png file
break;
default:
$im=false;
break;
}
$w = imagesx($im); //取得圖形的寬度
$h = imagesy($im); //取得圖形的高度
$im2 = ImageCreateTrueColor($_POST['w'], $_POST['h']);
//建立一個圖像寬度是新寬度 高度是新高度
imagecopyresampled($im2,$im,0,0,$_POST['x'],$_POST['y'],$_POST['w'],$_POST['h'],$_POST['w'],$_POST['h']);
$result_img = imagejpeg($im2,"after_save/".$_POST['img_original_filename']."_new.jpg",100);//另存新圖
//釋放記憶體空間
imagedestroy($im);
imagedestroy($im2);
?>
<img src="after_save/<?php echo $_POST['img_original_filename'];?>_new.jpg"/>
<?php
//save photo to database
$croped_photo = $_POST['img_original_filename']."_new.jpg";
$add_croped_image_sql = "UPDATE `images` SET `croped_photo` = '$croped_photo' WHERE `id`=1";
mysql_query($add_croped_image_sql)or die(mysql_error());
echo "儲存成功!";
?>
<a class="btn" href="javascript:void history.back();">Back,re-crop</a>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment