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,027

HOME > PHP > PHP Forum > class นี้มันดึงออกมาใช้ยังไงเหรอครับ พอดีว่าผมต้องแก้ด่วนครับ ปล.ของคนอื่น



 

class นี้มันดึงออกมาใช้ยังไงเหรอครับ พอดีว่าผมต้องแก้ด่วนครับ ปล.ของคนอื่น

 



Topic : 079523



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



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




Code (PHP)
<?php
class Image
{
  var $image_path = '';
  //holds the image path
  var $sizelimit_x = 250;
  //the limit of the image width
  var $sizelimit_y = 250;
  //the limit of the image height
  var $image_resource = '';
  //holds the image resource
  var $keep_proportions = true;
  //if true it keeps the image proportions when resized
  var $resized_resource = '';
  //holds the resized image resource
  var $hasGD = false;
  var $output = 'SAME';
  //can be JPG, GIF, PNG, or SAME (same will save as old type)
  
  function __construct()
  {
    if( function_exists('gd_info') ){ $this->hasGD = true; }  
  }
  
  function resize_image( $image_path )
  {
    if( $this->hasGD == false ){ return false; }
    //no GD installed on the server!
    
    list($img_width, $img_height, $img_type, $img_attr) = @getimagesize( $image_path );
    //this is going to get the image width, height, and format
    
    if( ( $img_width != 0 ) && ( $img_height != 0 ) )
    //make sure it was loaded correctly
    {
      switch( $img_type )
      {
        case 1:
          //GIF
          $this->image_resource = @imagecreatefromgif( $image_path );
		  $this->trancolor = @imagecolortransparent($this->image_resource);
          if( $this->output == 'SAME' ){ $this->output = 'GIF'; }
          break;
        case 2:
          //JPG
          $this->image_resource = @imagecreatefromjpeg( $image_path );
          if( $this->output == 'SAME' ){ $this->output = 'JPG'; }
          break;  
        case 3:
          //PNG
          $this->image_resource = @imagecreatefrompng( $image_path );
          if( $this->output == 'SAME' ){ $this->output = 'PNG'; }
      }
      if( $this->image_resource == '' ){ return false; }
      //it wasn't able to load the image
    }
    else{ return false; }
    //something happened!
    
    if( $this->keep_proportions == true )
    {
      if( ($img_width-$this->sizelimit_x) > ($img_height-$this->sizelimit_y) )
      {
      //if the width of the img is greater than the size limit we scale by width
        $scalex = ( $this->sizelimit_x / $img_width );
        $scaley = $scalex;
      }
      else
      //if the height of the img is greater than the size limit we scale by height
      {
        $scalex = ( $this->sizelimit_y / $img_height );
        $scaley = $scalex;
      }
	  
	  //don't make it so it streches the image
	  if( $scalex > 1 ){ $scalex = 1; }
      if( $scaley > 1 ){ $scaley = 1; }
    }
    else 
    {
      $scalex = ( $this->sizelimit_x / $img_width );
      $scaley = ( $this->sizelimit_y / $img_height );
      //just make the image fit the image size limit
      
      if( $scalex > 1 ){ $scalex = 1; }
      if( $scaley > 1 ){ $scaley = 1; }
      //don't make it so it streches the image
    }
    
    $new_width = $img_width * $scalex;
    $new_height = $img_height * $scaley;
    
    $this->resized_resource = @imagecreatetruecolor( $new_width, $new_height );
    //creates an image resource, with the width and height of the size limits (or new resized proportion )
    
    if( function_exists( 'imageantialias' )){ @imageantialias( $this->resized_resource, true ); }
    //helps in the quality of the image being resized
    
	if($img_type == 1) {
		//GIF
		$trnprt_indx = imagecolortransparent($this->image_resource);
		if($trnprt_indx >= 0) {
			//it's transparent
			$trnprt_color = imagecolorsforindex($this->image_resource, $trnprt_indx);
			$trnprt_indx = imagecolorallocate($this->resized_resource, $trnprt_color["red"], $trnprt_color["green"], $trnprt_color["blue"]);
			imagefill($this->resized_resource, 0, 0, $trnprt_indx);
			imagecolortransparent($this->resized_resource, $trnprt_indx);
		}
	}
	
    @imagecopyresampled( $this->resized_resource, $this->image_resource, 0, 0, 0, 0, $new_width, $new_height, $img_width, $img_height );
    //resize the iamge onto the resized resource
	
    @imagedestroy( $this->image_resource );
    //destory old image resource
    
    return true;
  }
  
  function save_resizedimage( $path, $name )
  {
    switch( strtoupper($this->output) )
    {
      case 'GIF':
        //GIF
        @imagegif( $this->resized_resource, $path . $name . '.gif' );
        break;
      case 'JPG':
        //JPG
        @imagejpeg( $this->resized_resource, $path . $name . '.jpg' );
        break;  
      case 'PNG':
        //PNG
        @imagepng( $this->resized_resource, $path . $name . '.png' );
    }
  }
  
  function output_resizedimage()
  {
    $the_time = time();
    header('Last-Modified: ' . date( 'D, d M Y H:i:s', $the_time ) . ' GMT'); 
    header('Cache-Control: public');
    
    switch( strtoupper($this->output) )
    {
      case 'GIF':
        //GIF
        header('Content-type: image/gif');
        @imagegif( $this->resized_resource );
        break;
      case 'JPG':
        //JPG
        header('Content-type: image/jpg');
        @imagejpeg( $this->resized_resource );
        break;  
      case 'PNG':
        //PNG
        header('Content-type: image/png');
        @imagepng( $this->resized_resource );
    }
  }
  
  function destroy_resizedimage()
  {
    @imagedestroy( $this->resized_resource );
    @imagedestroy( $this->image_resource );
  }
  
}


?>




Tag : PHP









ประวัติการแก้ไข
2012-06-08 10:24:06
Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2012-06-08 10:23:17 By : l3alLkisS View : 962 Reply : 5
 

 

No. 1



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

สมาชิกที่ใส่เสื้อไทยครีเอท

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

การเรียกใช้งานสร้าง instant ขึ้นมาดังนี้
Code (PHP)
$newimg = new Image();

ส่วนการเรียกใช้ member และ method เรียกใช้ได้นี้
Code (PHP)
$newimg->sizelimit_x="300"; //member
$newimg->output_resizedimage();//method







แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-06-08 10:59:19 By : narubet
 


 

No. 2



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

สมาชิกที่ใส่เสื้อไทยครีเอท Hall of Fame 2012

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

ลองดูน่ะครับ
Code (PHP)
<form enctype="multipart/form-data" name="upload" action="" method="post">
<input type="file" name="filename">
<input type="submit">
</form>
<?
$Image = new Image;

if(copy($_FILES["filename"]["tmp_name"],$_FILES['filename']['name'])){
	if($Image->resize_image($_FILES['filename']['name'])==true){
		if($Image->save_resizedimage('','resize_'.$_FILES['filename']['name'])){
			$Image->destroy_resizedimage();
		}
	}
}
?>

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-06-08 11:10:17 By : mangkunzo
 

 

No. 3



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



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


ทำไมขนาดรูปมันไม่ลดลงครับ งงเลย โค๊ดนี้
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-06-08 11:13:40 By : l3alLkisS
 


 

No. 4



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

สมาชิกที่ใส่เสื้อไทยครีเอท Hall of Fame 2012

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

ตอบความคิดเห็นที่ : 3 เขียนโดย : l3alLkisS เมื่อวันที่ 2012-06-08 11:13:40
รายละเอียดของการตอบ ::
ผมลองแล้วลดน่ะครับ ต้นฉบับ 38k เหลือแค่ 8k (จาก code ผมด้านบนน่ะ)

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-06-08 11:22:21 By : mangkunzo
 


 

No. 5



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



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


ยังไงก็ขอบคุณนะครับ แต่ภาพยังไม่ลดขนาดเลย
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-06-08 11:34:02 By : l3alLkisS
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : class นี้มันดึงออกมาใช้ยังไงเหรอครับ พอดีว่าผมต้องแก้ด่วนครับ ปล.ของคนอื่น
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ 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 02
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 อัตราราคา คลิกที่นี่