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 > php crop image รูปภาพ (อยากให้แสดงแบบ crop แต่รูปจริงไม่ต้อง crop)



 

php crop image รูปภาพ (อยากให้แสดงแบบ crop แต่รูปจริงไม่ต้อง crop)

 



Topic : 061715

Guest




ความต้องการของผมนะครับ ผมอยากได้ ฟังชั่น ที่เวลานำรูปมาแสดงแล้วให้ 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







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2011-06-15 21:43:08 By : i5z View : 4864 Reply : 4
 

 

No. 1



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

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

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

สร้างเป็นอีกไฟล์หนึ่งครับ แล้วเรียก


Code
<img src="view.php">


สร้างไฟล์ไว้ใน view ครับ


Go to : PHP Create Image






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2011-06-15 22:10:01 By : webmaster
 


 

No. 2

Guest


ขอบคุณมากครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2011-06-15 23:08:13 By : i5z
 

 

No. 3

Guest


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 &gt; $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 &lt; $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 : อะนัน
 


 

No. 4

Guest


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
 

   

ค้นหาข้อมูล


   
 

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