01.
<?php
02.
03.
function
croptofit(
$source_path
,
$desired_width
,
$desired_height
){
04.
05.
06.
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.
33.
34.
$temp_height
=
$desired_height
;
35.
$temp_width
= ( int ) (
$desired_height
*
$source_aspect_ratio
);
36.
}
37.
else
38.
{
39.
40.
41.
42.
$temp_width
=
$desired_width
;
43.
$temp_height
= ( int ) (
$desired_width
/
$source_aspect_ratio
);
44.
}
45.
46.
47.
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.
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.
80.
81.
82.
83.
header(
'Content-type: image/jpeg'
);
84.
imagejpeg(
$desired_gdim
);
85.
86.
87.
88.
89.
}
90.
91.
#using
92.
croptofit(
'pic/Music_Dancesm.jpg'
, 100, 100);
93.
?>