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 > ช่วยทำให้เป็น function ที พอดีไปได้โค้ด crop รูปมานะครับดีด้วย แต่อยากทำให้เป็นแบบ function นะครับ



 

ช่วยทำให้เป็น function ที พอดีไปได้โค้ด crop รูปมานะครับดีด้วย แต่อยากทำให้เป็นแบบ function นะครับ

 



Topic : 044361

Guest




พอดีไปได้โค้ด crop รูปมานะครับดีด้วย แต่อยากทำให้เป็นแบบ function นะครับ ใครพอทำได้บ้างครับ


Code (PHP)
<?php
  //----------------------------------------------------------------
  // Crop-to-fit PHP-GD
  // Revision 2 [2009-06-01]
  // Corrected aspect ratio of the output image
  //----------------------------------------------------------------

  define( 'DESIRED_IMAGE_WIDTH', 200 );
  define( 'DESIRED_IMAGE_HEIGHT', 100 );

  $source_path = $_FILES[ 'Image1' ][ 'tmp_name' ];

  //
  // Add file validation code here
  //

  list( $source_width, $source_height, $source_type ) = getimagesize( $source_path );

  switch ( $source_type )
  {
    case IMAGETYPE_GIF:
      $source_gdim = imagecreatefromgif( $source_path );
      break;

    case IMAGETYPE_JPEG:
      $source_gdim = imagecreatefromjpeg( $source_path );
      break;

    case IMAGETYPE_PNG:
      $source_gdim = imagecreatefrompng( $source_path );
      break;
  }

  $source_aspect_ratio = $source_width / $source_height;
  $desired_aspect_ratio = DESIRED_IMAGE_WIDTH / DESIRED_IMAGE_HEIGHT;

  if ( $source_aspect_ratio > $desired_aspect_ratio )
  {
    //
    // Triggered when source image is wider
    //
    $temp_height = DESIRED_IMAGE_HEIGHT;
    $temp_width = ( int ) ( DESIRED_IMAGE_HEIGHT * $source_aspect_ratio );
  }
  else
  {
    //
    // Triggered otherwise (i.e. source image is similar or taller)
    //
    $temp_width = DESIRED_IMAGE_WIDTH;
    $temp_height = ( int ) ( DESIRED_IMAGE_WIDTH / $source_aspect_ratio );
  }

  //
  // Resize the image into a temporary GD image
  //

  $temp_gdim = imagecreatetruecolor( $temp_width, $temp_height );
  imagecopyresampled(
    $temp_gdim,
    $source_gdim,
    0, 0,
    0, 0,
    $temp_width, $temp_height,
    $source_width, $source_height
  );

  //
  // Copy cropped region from temporary image into the desired GD image
  //

  $x0 = ( $temp_width - DESIRED_IMAGE_WIDTH ) / 2;
  $y0 = ( $temp_height - DESIRED_IMAGE_HEIGHT ) / 2;

  $desired_gdim = imagecreatetruecolor( DESIRED_IMAGE_WIDTH, DESIRED_IMAGE_HEIGHT );
  imagecopy(
    $desired_gdim,
    $temp_gdim,
    0, 0,
    $x0, $y0,
    DESIRED_IMAGE_WIDTH, DESIRED_IMAGE_HEIGHT
  );

  //
  // Render the image
  // Alternatively, you can save the image in file-system or database
  //

  header( 'Content-type: image/jpeg' );
  imagejpeg( $desired_gdim );

  //
  // Add clean-up code here
  //
?>




Tag : - - - -







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2010-06-22 15:15:45 By : พล View : 2934 Reply : 9
 

 

No. 1

Guest


เงียบกันหมดเลย






Date : 2010-06-22 19:40:10 By : พล
 


 

No. 2



โพสกระทู้ ( 11,835 )
บทความ ( 10 )

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

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


สมัครสมาชิกก่อน ผมจะช่วยตอบ อิอิ
Date : 2010-06-22 21:21:20 By : plakrim
 

 

No. 3



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



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


สมัครสมาชิกแล้วครับผม โค้ดตัวนี้มันครอปรูปได้ก็จริง แต่ว่ามันยังไม่ได้เซฟรูปไว้อีกไฟร์หนึ่งนะครับ ต้องการอัพแล้วเซฟรูปไว้แล้วเก็บรูปที่ครอปด้วยนะครับ
Date : 2010-06-23 07:28:22 By : looktevada
 


 

No. 4



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



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


กำลังรอความช่วยเหลือ อิอิ
Date : 2010-06-23 10:55:04 By : looktevada
 


 

No. 5



โพสกระทู้ ( 11,835 )
บทความ ( 10 )

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

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


Code (PHP)
<?php
//----------------------------------------------------------------
// Crop-to-fit PHP-GD
// Revision 2 [2009-06-01]
// Corrected aspect ratio of the output image
//----------------------------------------------------------------

function croptofit($source_files, $desired_width = 200, $desired_height = 100){}
$source_path = $source_files[ 'tmp_name' ];

//
// Add file validation code here
//

list( $source_width, $source_height, $source_type ) = getimagesize( $source_path );

switch ( $source_type )
{
case IMAGETYPE_GIF:
$source_gdim = imagecreatefromgif( $source_path );
break;

case IMAGETYPE_JPEG:
$source_gdim = imagecreatefromjpeg( $source_path );
break;

case IMAGETYPE_PNG:
$source_gdim = imagecreatefrompng( $source_path );
break;
}

$source_aspect_ratio = $source_width / $source_height;
$desired_aspect_ratio = $desired_width / $desired_height;

if ( $source_aspect_ratio > $desired_aspect_ratio )
{
//
// Triggered when source image is wider
//
$temp_height = $desired_height;
$temp_width = ( int ) ( $desired_height * $source_aspect_ratio );
}
else
{
//
// Triggered otherwise (i.e. source image is similar or taller)
//
$temp_width = $desired_width;
$temp_height = ( int ) ( $desired_width / $source_aspect_ratio );
}

//
// Resize the image into a temporary GD image
//

$temp_gdim = imagecreatetruecolor( $temp_width, $temp_height );
imagecopyresampled(
$temp_gdim,
$source_gdim,
0, 0,
0, 0,
$temp_width, $temp_height,
$source_width, $source_height
);

//
// Copy cropped region from temporary image into the desired GD image
//

$x0 = ( $temp_width - $desired_width ) / 2;
$y0 = ( $temp_height - $desired_height ) / 2;

$desired_gdim = imagecreatetruecolor( $desired_width, $desired_height );
imagecopy(
$desired_gdim,
$temp_gdim,
0, 0,
$x0, $y0,
$desired_width, $desired_height
);

//
// Render the image
// Alternatively, you can save the image in file-system or database
//

header( 'Content-type: image/jpeg' );
imagejpeg( $desired_gdim );

//
// Add clean-up code here
//
}

#using
croptofit($_FILES['image1']); ถ้าไม่ต้องการเปลี่ยน width, heidht
croptofit($_FILES['image1'], 500, 500); ถ้าต้องการเปลี่ยน width, heidht
?>

Date : 2010-06-23 14:56:35 By : plakrim
 


 

No. 6



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



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


ผมเอาไปใช้แล้ว ครอบไม่ได้เลยครับ ไม่erroe แต่ก็ไม่เกิดไรขึ้นเลย

โค้ดที่แก้ให้นะ ลืมเอาปีกกาตรง function ออกอันหนึ่งนะ ผมแก้เป็นแบบนี้แล้ว

Code (PHP)
<?php
//----------------------------------------------------------------
// Crop-to-fit PHP-GD
// Revision 2 [2009-06-01]
// Corrected aspect ratio of the output image
//----------------------------------------------------------------

function croptofit($source_files, $desired_width = 200, $desired_height = 100){
$source_path = $source_files[ 'tmp_name' ];

//
// Add file validation code here
//

list( $source_width, $source_height, $source_type ) = getimagesize( $source_path );

switch ( $source_type )
{
case IMAGETYPE_GIF:
$source_gdim = imagecreatefromgif( $source_path );
break;

case IMAGETYPE_JPEG:
$source_gdim = imagecreatefromjpeg( $source_path );
break;

case IMAGETYPE_PNG:
$source_gdim = imagecreatefrompng( $source_path );
break;
}

$source_aspect_ratio = $source_width / $source_height;
$desired_aspect_ratio = $desired_width / $desired_height;

if ( $source_aspect_ratio > $desired_aspect_ratio )
{
//
// Triggered when source image is wider
//
$temp_height = $desired_height;
$temp_width = ( int ) ( $desired_height * $source_aspect_ratio );
}
else
{
//
// Triggered otherwise (i.e. source image is similar or taller)
//
$temp_width = $desired_width;
$temp_height = ( int ) ( $desired_width / $source_aspect_ratio );
}

//
// Resize the image into a temporary GD image
//

$temp_gdim = imagecreatetruecolor( $temp_width, $temp_height );
imagecopyresampled(
$temp_gdim,
$source_gdim,
0, 0,
0, 0,
$temp_width, $temp_height,
$source_width, $source_height
);

//
// Copy cropped region from temporary image into the desired GD image
//

$x0 = ( $temp_width - $desired_width ) / 2;
$y0 = ( $temp_height - $desired_height ) / 2;

$desired_gdim = imagecreatetruecolor( $desired_width, $desired_height );
imagecopy(
$desired_gdim,
$temp_gdim,
0, 0,
$x0, $y0,
$desired_width, $desired_height
);

//
// Render the image
// Alternatively, you can save the image in file-system or database
//

header( 'Content-type: image/jpeg' );
imagejpeg( $desired_gdim );

//
// Add clean-up code here
//
}

#using
croptofit($_FILES['image1']); //ถ้าไม่ต้องการเปลี่ยน width, heidht
//croptofit($_FILES['image1'], 500, 500); //ถ้าต้องการเปลี่ยน width, heidht
?>

Date : 2010-06-23 16:01:20 By : looktevada
 


 

No. 7



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



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


ทำได้แล้วครับ
Date : 2010-06-23 18:53:22 By : looktevada
 


 

No. 8



โพสกระทู้ ( 11,835 )
บทความ ( 10 )

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

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


จะย่อแบบนั้นก็ไม่บอก ผมก็ทำเหมือนที่ให้มาอะ
Date : 2010-06-23 22:12:48 By : plakrim
 


 

No. 9



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



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


มีแบบให้มัน processing รูป ให้มันเลือกครอปเฉพาะตรงที่เป็นหน้าคนได้ไหมครับ
Date : 2010-06-24 13:00:26 By : looktevada
 

   

ค้นหาข้อมูล


   
 

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