|
 |
|
พี่ๆทั้งหลาย ช่วยดูโค๊ด Resize ให้หน่อยครับ imagecreatefrompng , imagecopyresampled , imagecreatetruecolor |
|
 |
|
|
 |
 |
|
Code (PHP)
<?php
$photo=$_FILES['img_prd']['tmp_name'];
$photo_name=$_FILES['img_prd']['name'];
$photo_size=$_FILES['img_prd']['size'];
$photo_type=$_FILES['img_prd']['type'];
$flie_upload=$_FILES['file_upload_cai']['tmp_name'];
$flie_upload_name=$_FILES['file_upload_cai']['name'];
$flie_upload_size=$_FILES['file_upload_cai']['size'];
$flie_upload_type=$_FILES['file_upload_cai']['type'];
if(($id_upload_cai=="")or ($code_cai=="") or ($name_upload_cai=="") or ($price_cai=="")) {
echo "<br><br><br><div align=\"center\"><H3 class=\"txtred\">ERROR : Please fill all the form !!! </H3><br><br><br>";
echo "</td><td background=\"images/box05.gif\"> </td>
</tr>
<tr>
<td width=\"10\" height=\"8\" ><img src=\"images/box06.gif\" width=\"10\" height=\"8\" /></td>
<td height=\"8\" background=\"images/box07.gif\"></td>
<td width=\"10\" height=\"8\"><img src=\"images/box08.gif\" width=\"10\" height=\"8\" /></td>
</tr></table></td></tr></table></td></tr>";
echo "<tr>
<td colspan=\"2\"> </td>
</tr>
<tr>
<td height=\"54\" colspan=\"2\" background=\"images/bgfoot.gif\"><div align=\"center\"><img src=\"images/txtfoot.gif\" width=\"300\" height=\"54\" border=\"0\" usemap=\"#Map\" /></div></td>
</tr>
</table>";
die();
}
if(!empty($photo)){
if ($photo_size > $filesize){ //check file not bigger than 500KB
unlink($photo);
echo "<br><br><br><div align=\"center\"><H3 class=\"txtred\">ERROR : The file size not bigger than 500KB !!! </H3><br><br><br>";
echo "</td><td background=\"images/box05.gif\"> </td>
</tr>
<tr>
<td width=\"10\" height=\"8\" ><img src=\"images/box06.gif\" width=\"10\" height=\"8\" /></td>
<td height=\"8\" background=\"images/box07.gif\"></td>
<td width=\"10\" height=\"8\"><img src=\"images/box08.gif\" width=\"10\" height=\"8\" /></td>
</tr></table></td></tr></table></td></tr>";
echo "<tr>
<td colspan=\"2\"> </td>
</tr>
<tr>
<td height=\"54\" colspan=\"2\" background=\"images/bgfoot.gif\"><div align=\"center\"><img src=\"images/txtfoot.gif\" width=\"300\" height=\"54\" border=\"0\" usemap=\"#Map\" /></div></td>
</tr>
</table>";
die();
}
if (( ereg("^image/pjpeg",$photo_type))or(ereg("^image/png",$photo_type))){
}else{
if($photo<>''){
unlink($photo);
}
echo "<br><br><br><div align=\"center\"><H3 class=\"txtred\">ERROR : *.jpg or *.png only !!! </H3><br><br><br>";
echo "</td><td background=\"images/box05.gif\"> </td>
</tr>
<tr>
<td width=\"10\" height=\"8\" ><img src=\"images/box06.gif\" width=\"10\" height=\"8\" /></td>
<td height=\"8\" background=\"images/box07.gif\"></td>
<td width=\"10\" height=\"8\"><img src=\"images/box08.gif\" width=\"10\" height=\"8\" /></td>
</tr></table></td></tr></table></td></tr>";
echo "<tr>
<td colspan=\"2\"> </td>
</tr>
<tr>
<td height=\"54\" colspan=\"2\" background=\"images/bgfoot.gif\"><div align=\"center\"><img src=\"images/txtfoot.gif\" width=\"300\" height=\"54\" border=\"0\" usemap=\"#Map\" /></div></td>
</tr>
</table>";
die();
}
$passw = random_password(7);
$filenewcon = strstr($photo_name,'.');
$now = date("Dgis");
$img_prd = "$passw$now$filenewcon";
$original_photo = $photo;
$uploaddir = $dirprd;
$w_want = $w_prd;
$h_want = $h_prd;
$thumbdir = $dirprdr;
$w_want_re = $w_prdr;
$h_want_re = $h_prdr;
$uploadfile = $uploaddir. basename($img_prd);
$thumbfile = $thumbdir. basename($img_prd);
list($w_original,$h_original) = getimagesize($original_photo);
//resize normal photo
if(($w_original<$w_want)and($h_original<$h_want)){
$w_edit=$w_original;
$h_edit=$h_original;
}else{
if($w_original>=$w_want){
$w_edit=$w_want;
$percent_edit =$w_edit/$w_original;
$h_edit=$h_original*$percent_edit;
if($h_edit>$h_want){
$h_edit=$h_want;
$percent_edit =$h_edit/$h_original;
$w_edit=$w_original*$percent_edit;
}
}else if($h_original>=$h_want){
$h_edit=$h_want;
$percent_edit =$h_edit/$h_original;
$w_edit=$w_original*$percent_edit;
if($w_edit>$w_want){
$w_edit=$w_want;
$percent_edit =$w_edit/$w_original;
$h_edit=$h_original*$percent_edit;
}
}
}
$img_e1=imagecreatetruecolor($w_edit,$h_edit);
if (ereg ("^image/pjpeg",$photo_type)) {
$img_e2=imagecreatefromjpeg($original_photo);
}else if (ereg ("^image/png",$photo_type)) {
$img_e2=imagecreatefrompng($original_photo);
}
imagecopyresampled($img_e1,$img_e2,0,0,0,0,$w_edit,$h_edit,$w_original,$h_original);
imagejpeg($img_e1, $uploadfile, $quality);
imagedestroy($img_e1);
imagedestroy($img_e2);
//resize normal photo
//resize thumb photo
if(($w_original<$w_want_re)and($h_original<$h_want_re)){
$w_edit=$w_original;
$h_edit=$h_original;
}else{
if($w_original>=$w_want_re){
$w_edit=$w_want_re;
$percent_edit =$w_edit/$w_original;
$h_edit=$h_original*$percent_edit;
if($h_edit>$h_want_re){
$h_edit=$h_want_re;
$percent_edit =$h_edit/$h_original;
$w_edit=$w_original*$percent_edit;
}
}else if($h_original>=$h_want_re){
$h_edit=$h_want_re;
$percent_edit =$h_edit/$h_original;
$w_edit=$w_original*$percent_edit;
if($w_edit>$w_want_re){
$w_edit=$w_want_re;
$percent_edit =$w_edit/$w_original;
$h_edit=$h_original*$percent_edit;
}
}
}
$img_e1=imagecreatetruecolor($w_edit,$h_edit);
if (ereg ("^image/pjpeg",$photo_type)) {
$img_e2=imagecreatefromjpeg($original_photo);
}else if (ereg ("^image/png",$photo_type)) {
$img_e2=imagecreatefrompng($original_photo);
}
imagecopyresampled($img_e1,$img_e2,0,0,0,0,$w_edit,$h_edit,$w_original,$h_original);
imagejpeg($img_e1, $thumbfile, $quality);
imagedestroy($img_e1);
imagedestroy($img_e2);
//resize thumb photo
?>
ผลลัพท์ที่ออกมานะครับ

Tag : - - - -

|
|
 |
 |
 |
 |
Date :
2009-08-10 23:44:16 |
By :
TonNgiew |
View :
1099 |
Reply :
2 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตัวแปร "$img_e2" มีปัญหาแน่ๆครับ ลองเช็คค่าดู
|
 |
 |
 |
 |
Date :
2009-08-11 11:12:11 |
By :
... |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองสังเกตดูครับว่า $img_e1 และ $img_e2 ไม่ได้ define ไว้แต่แรกพอมาถึงตอนที่
if (ereg ("^image/pjpeg",$photo_type)) {
154.
$img_e2=imagecreatefromjpeg($original_photo);
155.
}else if (ereg ("^image/png",$photo_type)) {
156.
$img_e2=imagecreatefrompng($original_photo);
157.
}
จึงทำให้มีเพียง $img_e1 หรือไม่ก็ $img_e2
ซึ่งมันมีปัญหาแน่หากคุณทำารใช้ function แบบนี้
imagecopyresampled($img_e1,$img_e2,0,0,0,0,$w_edit,$h_edit,$w_original,$h_original);
หรือ
imagedestroy($img_e2); //โดยที่มีเพียง $img_e1 แต่ $img_e2 เมื่อไม่มีการกำหนดค่าตัว PHP จะกำหนดค่ามาตราฐานให้ซึ่งก็คือค่า NULL
ซึ่งจาก error ที่เห็นถ้าคุณลอง Browse ไฟล์รูป หรือไฟล์ประเภทอื่นๆเข้าไปที่ไม่ใช่ .jpg หรือ .png จะมี error เพิ่มขึ้นมาอีกอย่างแน่นอน
สรุป
การป้อนตัวแปรที่ไม่ได้ทำการ define ค่าเอาไว้เข้าไป มันจะเหมือนคุณป้อนค่า NULL เข้าไปซึ่งตัวฟังก์ชั่นได้กำหนดรูปแบบของตัวแปรตัวนั้นไว้แล้วมันจึง error นี่เป็น error ลักษณะของคนที่ เขียน และ ใช้งาน function ไม่เป็นครับ คุณต้องไปศึกษาเรื่องพวกนี้เพิ่มเติมคุณถึงจะแก้ได้
|
 |
 |
 |
 |
Date :
2009-08-11 11:23:03 |
By :
... |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 00
|