|
 |
|
ช่วยดูหน่อย function crop รูป ตัวนี้ พอ crop รูป เสร็จแล้ว ทำไมไปดูในไฟล์รูปจริง ขนาดยังเท่าเดิม |
|
 |
|
|
 |
 |
|
โค้ดตัวนี้ได้มาจากที่อื่นนะครับ มันก็ครอบรูปได้นะ แต่พอเข้าไปดูในพาท รูปจริง ขนากก็ยังเท่าเดิม แต่เวลามันแสดง กับได้ขนาดที่ต้องการครอบ
นี้โค้ดครับ
Code (PHP)
<?php
function croptofit($source_path, $desired_width, $desired_height){
//
// 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('pic/Music_Dancesm.jpg', 100, 100);
?>
Tag : - - - -

|
|
 |
 |
 |
 |
Date :
2010-06-23 20:18:27 |
By :
looktevada |
View :
3370 |
Reply :
2 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ทำได้แล้วครับ อิอิ ใครอยากจะเอาไปใช้ก็ได้นะไม่ว่ากัน
|
 |
 |
 |
 |
Date :
2010-06-23 20:21:52 |
By :
looktevada |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 02
|