Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,028

HOME > PHP > PHP Forum > พี่ๆทั้งหลาย ช่วยดูโค๊ด Resize ให้หน่อยครับ imagecreatefrompng , imagecopyresampled , imagecreatetruecolor



 

พี่ๆทั้งหลาย ช่วยดูโค๊ด Resize ให้หน่อยครับ imagecreatefrompng , imagecopyresampled , imagecreatetruecolor

 



Topic : 030181



โพสกระทู้ ( 8 )
บทความ ( 0 )



สถานะออฟไลน์




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\">&nbsp;</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\">&nbsp;</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\">&nbsp;</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\">&nbsp;</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\">&nbsp;</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\">&nbsp;</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
?>


ผลลัพท์ที่ออกมานะครับ

11



Tag : - - - -







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2009-08-10 23:44:16 By : TonNgiew View : 1069 Reply : 2
 

 

No. 1

Guest


ตัวแปร "$img_e2" มีปัญหาแน่ๆครับ ลองเช็คค่าดู






Date : 2009-08-11 11:12:11 By : ...
 


 

No. 2

Guest


ลองสังเกตดูครับว่า $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 : ...
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : พี่ๆทั้งหลาย ช่วยดูโค๊ด Resize ให้หน่อยครับ imagecreatefrompng , imagecopyresampled , imagecreatetruecolor
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 03
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่