|
|
|
ช่วยทำให้เป็น function ที พอดีไปได้โค้ด crop รูปมานะครับดีด้วย แต่อยากทำให้เป็นแบบ function นะครับ |
|
|
|
|
|
|
|
พอดีไปได้โค้ด 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 : - - - -
|
|
|
|
|
|
Date :
2010-06-22 15:15:45 |
By :
พล |
View :
2998 |
Reply :
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
เงียบกันหมดเลย
|
|
|
|
|
Date :
2010-06-22 19:40:10 |
By :
พล |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
สมัครสมาชิกก่อน ผมจะช่วยตอบ อิอิ
|
|
|
|
|
Date :
2010-06-22 21:21:20 |
By :
plakrim |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
สมัครสมาชิกแล้วครับผม โค้ดตัวนี้มันครอปรูปได้ก็จริง แต่ว่ามันยังไม่ได้เซฟรูปไว้อีกไฟร์หนึ่งนะครับ ต้องการอัพแล้วเซฟรูปไว้แล้วเก็บรูปที่ครอปด้วยนะครับ
|
|
|
|
|
Date :
2010-06-23 07:28:22 |
By :
looktevada |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
กำลังรอความช่วยเหลือ อิอิ
|
|
|
|
|
Date :
2010-06-23 10:55:04 |
By :
looktevada |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ผมเอาไปใช้แล้ว ครอบไม่ได้เลยครับ ไม่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 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ทำได้แล้วครับ
|
|
|
|
|
Date :
2010-06-23 18:53:22 |
By :
looktevada |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
จะย่อแบบนั้นก็ไม่บอก ผมก็ทำเหมือนที่ให้มาอะ
|
|
|
|
|
Date :
2010-06-23 22:12:48 |
By :
plakrim |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
มีแบบให้มัน processing รูป ให้มันเลือกครอปเฉพาะตรงที่เป็นหน้าคนได้ไหมครับ
|
|
|
|
|
Date :
2010-06-24 13:00:26 |
By :
looktevada |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 00
|