Link Web: http://www.thaicreate.com/php/php-upload-resize-image.html
CÁCH 1: CHỈNH SỬA HÌNH NHỎ HÌNH LỚN
phpUploadResize.html
CÁCH 1: CHỈNH SỬA HÌNH NHỎ HÌNH LỚN
phpUploadResize.html
<html>
<head>
<title>ThaiCreate.Com PHP Upload Resize</title>
</head>
<body>
<form action="phpUploadResize.php" method="post" enctype="multipart/form-data" name="frmMain">
<table width="343" border="1">
<tr>
<td>Upload</td>
<td><input name="fileUpload" type="file"></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</form>
</body>
</html>
phpUploadResize.php
<html>
<head>
<title>ThaiCreate.Com PHP Upload Resize</title>
</head>
<body>
<?php
if(trim($_FILES["fileUpload"]["tmp_name"]) != ""){
$images = $_FILES["fileUpload"]["tmp_name"];
$new_images = "Thumbnails_".$_FILES["fileUpload"]["name"];
copy($_FILES["fileUpload"]["tmp_name"],"MyResize/".$_FILES["fileUpload"]["name"]);
$width=100; //*** Fix Width & Heigh (Autu caculate) ***//
$size=GetimageSize($images);
$height=round($width*$size[1]/$size[0]);
$images_orig = ImageCreateFromJPEG($images);
$photoX = ImagesX($images_orig);
$photoY = ImagesY($images_orig);
$images_fin = ImageCreateTrueColor($width, $height);.
ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY);
ImageJPEG($images_fin,"MyResize/".$new_images);
ImageDestroy($images_orig);
ImageDestroy($images_fin);
}
?>
<b>Original Size</b><br>
<img src="<?php echo "MyResize/".$_FILES["fileUpload"]["name"];?>">
<hr>
<b>New Resize</b><br>
<img src="<?php echo "MyResize/".$new_images;?>">
</body>
</html>
#*********************************************************#
CÁCH 2: CHỈNH SỬA HÌNH NHỎ HÌNH LỚN
link web: https://stackoverflow.com/questions/18805497/php-resize-image-on-upload
define ("MAX_SIZE","10000");
$errors=0;
$image =$_FILES["fileField"]["name"];
$uploadedfile = $_FILES['fileField']['tmp_name'];
if($image){
$filename = stripslashes($_FILES['fileField']['name']);
$extension = strtolower(getExtension($filename));
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")){
echo ' Unknown Image extension ';
$errors=1;
} else{
$newname = "$product_cn."."$extension";
$size=filesize($_FILES['fileField']['tmp_name']);
if ($size > MAX_SIZE*1024){
echo "You have exceeded the size limit";
$errors=1;
}
if($extension=="jpg" || $extension=="jpeg" ){
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);
}else if($extension=="png"){
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefrompng($uploadedfile);
}else{
$src = imagecreatefromgif($uploadedfile);
}
list($width,$height)=getimagesize($uploadedfile);
$newwidth=60;
$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);
$newwidth1=25;
$newheight1=($height/$width)*$newwidth1;
$tmp1=imagecreatetruecolor($newwidth1,$newheight1);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight, $width,$height);
imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1, $width,$height);
$filename = "../products_images/".$newname;
$filename1 = "../products_images/thumbs/".$newname;
imagejpeg($tmp,$filename,100); //file name also indicates the folder where to save it to
imagejpeg($tmp1,$filename1,100);
imagedestroy($src);
imagedestroy($tmp);
imagedestroy($tmp1);
}
}
#*********************************************************#
CÁCH 3:
link web: https://formstone.it/components/upload/demo/
#*********************************************************#
CÁCH 2: CHỈNH SỬA HÌNH NHỎ HÌNH LỚN
link web: https://stackoverflow.com/questions/18805497/php-resize-image-on-upload
define ("MAX_SIZE","10000");
$errors=0;
$image =$_FILES["fileField"]["name"];
$uploadedfile = $_FILES['fileField']['tmp_name'];
if($image){
$filename = stripslashes($_FILES['fileField']['name']);
$extension = strtolower(getExtension($filename));
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")){
echo ' Unknown Image extension ';
$errors=1;
} else{
$newname = "$product_cn."."$extension";
$size=filesize($_FILES['fileField']['tmp_name']);
if ($size > MAX_SIZE*1024){
echo "You have exceeded the size limit";
$errors=1;
}
if($extension=="jpg" || $extension=="jpeg" ){
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);
}else if($extension=="png"){
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefrompng($uploadedfile);
}else{
$src = imagecreatefromgif($uploadedfile);
}
list($width,$height)=getimagesize($uploadedfile);
$newwidth=60;
$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);
$newwidth1=25;
$newheight1=($height/$width)*$newwidth1;
$tmp1=imagecreatetruecolor($newwidth1,$newheight1);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight, $width,$height);
imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1, $width,$height);
$filename = "../products_images/".$newname;
$filename1 = "../products_images/thumbs/".$newname;
imagejpeg($tmp,$filename,100); //file name also indicates the folder where to save it to
imagejpeg($tmp1,$filename1,100);
imagedestroy($src);
imagedestroy($tmp);
imagedestroy($tmp1);
}
}
#*********************************************************#
CÁCH 3:
link web: https://formstone.it/components/upload/demo/
Bài viết hay quá, mời các bạn ghé thăm sản phẩm chăn ga gối đệm Sông Hồng của chúng tôi!
Trả lờiXóa.........................................
CÔNG TY TNHH SẢN XUẤT VÀ ĐẦU TƯ MINH PHONG
Địa chỉ: 113 Nguyễn Trãi, Q. Thanh Xuân, TP. Hà Nội
Điện thoại: 02462 605 064 - Hotline: 1900 636 746
Email: entershopping.com.vn@gmail.com - Website: entershopping.vn
Ở trên là những đoạn code tôi thường sử dụng, ngoài ra mọi người còn có thể trải nghiệm chuyển đổi hình ảnh jpg sang png để giúp hình ảnh chất lượng, Tôi ưu công việc chuyển đổi ảnh sang định dạng khác tại: jpg4png.com
Trả lờiXóa