 |
php crop image รูปภาพ (อยากให้แสดงแบบ crop แต่รูปจริงไม่ต้อง crop) |
|
 |
|
|
 |
 |
|
ความต้องการของผมนะครับ ผมอยากได้ ฟังชั่น ที่เวลานำรูปมาแสดงแล้วให้ crop รูปด้วย ...... แต่ไม่ต้องทำการ crop รูปจริง
ผมไปเจออยู่ code หนึ่งครับ น่าจะได้มาจาก thaicreate นี่แหล่ะ (เกือบใช่แบบที่ต้องการล่ะครับ code นี้ สามารถแสดงแบบ crop และไม่ทำการ crop รูปจริง ) แต่มันติดตรงที่ว่า เวลาใช้โค๊ชนี้ มันจะแสดงเฉพาะ ภาพที่ crop มา ส่วนข้อมูลอื่นๆ ที่ผมทำไว้ มันไม่แสดงครับ
เช่นผมจะทำการ crop รูปรถ พร้อมทั้งดึงข้อมูลรถมาแสดง แต่มันไม่ดึงข้อมูลรถ ออกมาแสดงด้วยครับ
ผมลองดูคราวๆ มันติดที่ตรงนี้ครับ
ถ้าเอาโค๊ช crop รูปวางไว้บนสุด มันจะแสดงรูป แต่ไม่แสดงข้อมูลอื่นๆ
Code (PHP)
header( 'Content-type: image/jpeg' );
imagejpeg( $desired_gdim );
แต่ถ้าเอา สองบรรทัดบนนี้ออกมันจะแสดงข้อมูลได้ปกติ แต่โค๊ช 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('mygirl.jpg', 300, 100);
?>
Tag : PHP, MySQL
|
|
 |
 |
 |
 |
Date :
2011-06-15 21:43:08 |
By :
i5z |
View :
5035 |
Reply :
4 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณมากครับ 
|
 |
 |
 |
 |
Date :
2011-06-15 23:08:13 |
By :
i5z |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
crop.php
Code (PHP)
<?
header ("Content-type: image/jpeg");
$file_name=$_GET['f'];
$crop_height=$_GET['h'];
$crop_width=$_GET['w'];
$file_type= explode('.', $file_name);
$file_type = $file_type[count($file_type) -1];
$file_type=strtolower($file_type);
$original_image_size = getimagesize($file_name);
$original_width = $original_image_size[0];
$original_height = $original_image_size[1];
if($file_type=='jpg')
{
$original_image_gd = imagecreatefromjpeg($file_name);
}
if($file_type=='gif')
{ $original_image_gd = imagecreatefromgif($file_name);
}
if($file_type=='png')
{
$original_image_gd = imagecreatefrompng($file_name);
}
$cropped_image_gd = imagecreatetruecolor($crop_width, $crop_height);
$wm = $original_width /$crop_width;
$hm = $original_height /$crop_height;
$h_height = $crop_height/2;
$w_height = $crop_width/2;
if($original_width > $original_height )
{
$adjusted_width =$original_width / $hm;
$half_width = $adjusted_width / 2;
$int_width = $half_width - $w_height;
imagecopyresampled($cropped_image_gd ,$original_image_gd ,-$int_width,0,0,0, $adjusted_width, $crop_height, $original_width , $original_height );
}
elseif(($original_width < $original_height ) || ($original_width == $original_height ))
{
$adjusted_height = $original_height / $wm;
$half_height = $adjusted_height / 2;
$int_height = $half_height - $h_height;
imagecopyresampled($cropped_image_gd , $original_image_gd ,0,-$int_height,0,0, $crop_width, $adjusted_height, $original_width , $original_height );
}
else {
imagecopyresampled($cropped_image_gd , $original_image_gd ,0,0,0,0, $crop_width, $crop_height, $original_width , $original_height );
}
imagejpeg($cropped_image_gd);
?>
เรียกใช้
Code
<img src="crop.php?h=100&w=100&f=jubpas.jpg" />
|
 |
 |
 |
 |
Date :
2011-06-17 13:01:19 |
By :
อะนัน |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code on Link :
Code
Private Sub button1_Click(sender As Object, e As EventArgs)
Dim folderName As String = "c:/YGPic.bmp"
Dim ygPic As YGpic = YGFolder.LoadImageFolder(folderName)
ImageManipulating.ApplyImageCropping(ygPic, 50, 60, 200, 300)
YGFolder.StoreImageFolder(ygPic, "c:/ygpic.bmp", New BMPCreator())
End Sub
|
 |
 |
 |
 |
Date :
2014-04-29 11:14:30 |
By :
arronlee |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|