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,038

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


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

 
Topic : 061715

Guest



ความต้องการของผมนะครับ ผมอยากได้ ฟังชั่น ที่เวลานำรูปมาแสดงแล้วให้ crop รูปด้วย ...... แต่ไม่ต้องทำการ crop รูปจริง

ผมไปเจออยู่ code หนึ่งครับ น่าจะได้มาจาก thaicreate นี่แหล่ะ (เกือบใช่แบบที่ต้องการล่ะครับ code นี้ สามารถแสดงแบบ crop และไม่ทำการ crop รูปจริง ) แต่มันติดตรงที่ว่า เวลาใช้โค๊ชนี้ มันจะแสดงเฉพาะ ภาพที่ crop มา ส่วนข้อมูลอื่นๆ ที่ผมทำไว้ มันไม่แสดงครับ

เช่นผมจะทำการ crop รูปรถ พร้อมทั้งดึงข้อมูลรถมาแสดง แต่มันไม่ดึงข้อมูลรถ ออกมาแสดงด้วยครับ


ผมลองดูคราวๆ มันติดที่ตรงนี้ครับ

ถ้าเอาโค๊ช crop รูปวางไว้บนสุด มันจะแสดงรูป แต่ไม่แสดงข้อมูลอื่นๆ

Code (PHP)
1.header( 'Content-type: image/jpeg' );
2.imagejpeg( $desired_gdim );


แต่ถ้าเอา สองบรรทัดบนนี้ออกมันจะแสดงข้อมูลได้ปกติ แต่โค๊ช crop รูปมันไม่ทำงานครับ


ไม่ทราบจะแก้ไขยังไงดี อยากแสดงข้อมูลอื่นๆ และใช้งานฟังชั่น crop รูปได้ด้วย

Code (PHP)
01.<?php
02. 
03.function croptofit($source_path, $desired_width, $desired_height){
04. 
05.//
06.// Add file validation code here
07.//
08. 
09.list( $source_width, $source_height, $source_type ) = getimagesize( $source_path );
10. 
11.switch ( $source_type )
12.{
13.case IMAGETYPE_GIF:
14.$source_gdim = imagecreatefromgif( $source_path );
15.break;
16. 
17.case IMAGETYPE_JPEG:
18.$source_gdim = imagecreatefromjpeg( $source_path );
19.break;
20. 
21.case IMAGETYPE_PNG:
22.$source_gdim = imagecreatefrompng( $source_path );
23.break;
24.}
25. 
26.$source_aspect_ratio = $source_width / $source_height;
27.$desired_aspect_ratio = $desired_width / $desired_height;
28. 
29.if ( $source_aspect_ratio > $desired_aspect_ratio )
30.{
31.//
32.// Triggered when source image is wider
33.//
34.$temp_height = $desired_height;
35.$temp_width = ( int ) ( $desired_height * $source_aspect_ratio );
36.}
37.else
38.{
39.//
40.// Triggered otherwise (i.e. source image is similar or taller)
41.//
42.$temp_width = $desired_width;
43.$temp_height = ( int ) ( $desired_width / $source_aspect_ratio );
44.}
45. 
46.//
47.// Resize the image into a temporary GD image
48.//
49. 
50.$temp_gdim = imagecreatetruecolor( $temp_width, $temp_height );
51.imagecopyresampled(
52.$temp_gdim,
53.$source_gdim,
54.0, 0,
55.0, 0,
56.$temp_width, $temp_height,
57.$source_width, $source_height
58.);
59. 
60.//
61.// Copy cropped region from temporary image into the desired GD image
62.//
63. 
64.$x0 = ( $temp_width - $desired_width ) / 2;
65.$y0 = ( $temp_height - $desired_height ) / 2;
66. 
67.$desired_gdim = imagecreatetruecolor( $desired_width, $desired_height );
68.imagecopy(
69.$desired_gdim,
70.$temp_gdim,
71.0, 0,
72.$x0, $y0,
73.$desired_width, $desired_height
74.);
75. 
76. 
77. 
78.//
79.// Render the image
80.// Alternatively, you can save the image in file-system or database
81.//
82. 
83.header( 'Content-type: image/jpeg' );
84.imagejpeg( $desired_gdim );
85. 
86.//
87.// Add clean-up code here
88.//
89.}
90. 
91.#using
92.croptofit('mygirl.jpg', 300, 100);
93.?>




Tag : PHP, MySQL

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

 

No. 1



โพสกระทู้ ( 74,059 )
บทความ ( 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)
01.<?
02.header ("Content-type: image/jpeg");
03.$file_name=$_GET['f'];
04.$crop_height=$_GET['h'];
05.$crop_width=$_GET['w'];
06.$file_type= explode('.', $file_name);
07.$file_type = $file_type[count($file_type) -1];
08.$file_type=strtolower($file_type);
09. 
10.$original_image_size = getimagesize($file_name);
11.$original_width = $original_image_size[0];
12.$original_height = $original_image_size[1];
13. 
14.if($file_type=='jpg')
15.{
16.$original_image_gd = imagecreatefromjpeg($file_name);
17.}
18. 
19.if($file_type=='gif')
20.{ $original_image_gd = imagecreatefromgif($file_name);
21.}
22. 
23.if($file_type=='png')
24.{
25.$original_image_gd = imagecreatefrompng($file_name);
26.}
27. 
28.$cropped_image_gd = imagecreatetruecolor($crop_width, $crop_height);
29.$wm = $original_width /$crop_width;
30.$hm = $original_height /$crop_height;
31.$h_height = $crop_height/2;
32.$w_height = $crop_width/2;
33. 
34.if($original_width &gt; $original_height )
35.{
36.$adjusted_width =$original_width / $hm;
37.$half_width = $adjusted_width / 2;
38.$int_width = $half_width - $w_height;
39. 
40.imagecopyresampled($cropped_image_gd ,$original_image_gd ,-$int_width,0,0,0, $adjusted_width, $crop_height, $original_width , $original_height );
41.}
42.elseif(($original_width &lt; $original_height ) || ($original_width == $original_height ))
43.{
44.$adjusted_height = $original_height / $wm;
45.$half_height = $adjusted_height / 2;
46.$int_height = $half_height - $h_height;
47. 
48.imagecopyresampled($cropped_image_gd , $original_image_gd ,0,-$int_height,0,0, $crop_width, $adjusted_height, $original_width , $original_height );
49.}
50.else {
51. 
52.imagecopyresampled($cropped_image_gd , $original_image_gd ,0,0,0,0, $crop_width, $crop_height, $original_width , $original_height );
53.}
54.imagejpeg($cropped_image_gd);
55. 
56.?>


เรียกใช้

Code
1.<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
01.Private Sub button1_Click(sender As Object, e As EventArgs)
02.   Dim folderName As String = "c:/YGPic.bmp"
03. 
04.   Dim ygPic As YGpic = YGFolder.LoadImageFolder(folderName)
05.   ImageManipulating.ApplyImageCropping(ygPic, 50, 60, 200, 300)
06. 
07.   YGFolder.StoreImageFolder(ygPic, "c:/ygpic.bmp", New BMPCreator())
08.        
09.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 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)





ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2025 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่