0

Last night I asked how to use a url as an argument in 'imagecreatefrom...()' without using fopen(). cURL was presented as my answer, but when I try plugging it in, as well as messing around with some suggestions I found around the web, I just can't get it to work.

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://curious.astro.cornell.edu/images/xraysun.gif'); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); 
$data = curl_exec($ch);
curl_close($ch);

$image = imagecreatefromstring($data);

$thumb_width = 200;
$thumb_height = 150;

$width = imagesx($image);
$height = imagesy($image);

$original_aspect = $width / $height;
$thumb_aspect = $thumb_width / $thumb_height;

if ( $original_aspect >= $thumb_aspect )
{
// If image is wider than thumbnail (in aspect ratio sense)
$new_height = $thumb_height;
$new_width = $width / ($height / $thumb_height);
}
else
{
// If the thumbnail is wider than the image
$new_width = $thumb_width;
$new_height = $height / ($width / $thumb_width);
}

$thumb = imagecreatetruecolor( $thumb_width, $thumb_height );

// Resize and crop
imagecopyresampled($thumb,
$image,
0 - ($new_width - $thumb_width) / 2, // Center the image horizontally
0 - ($new_height - $thumb_height) / 2, // Center the image vertically
0, 0,
$new_width, $new_height,
$width, $height);

imagejpeg($thumb);


This is what imagejpeg() outputs:

����JFIF��>CREATOR: gd-jpeg v1.0 (using IJG JPEG v80), default quality ��C      $.' ",#(7),01444'9=82<.342��C     2!!22222222222222222222222222222222222222222222222222����"��   ���}!1AQa"q2���#B��R��$3br� %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz��������������������������������������������������������������������������� ���w!1AQaq"2�B����    #3R�br� $4�%�&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz��������������������������������������������������������������������������?��)�P< ����ZxZUZ�R����4GR�UJ�R����2���S�>�uw�K #��6��b1�V1��#�85k�+nIO�hZh�7.�e�r;Weoḭrȹ����w-R����ӕS��>y�;复����{�B��F z��(ZF�$��U��YIh�;�7P)�F�Cs&G�@�>���^OY�El��|�u�H&��mͶ9B���Zq��(\3�<�;1���R��":ȳ���'>��s�MD����e/���*�$�o��Z41��`7�Ӛ����Ȗ#�Ðx��A������t��T�ܮ4rWp��{V|��]}��N�f&�%����\Bcb�#��[]MXƵ>ddȘ�kBH� �A3ƫAܢ�Q���*N��g�R�Eb)�T�j2*�9���)�QL�ĸH��J��l���z�N�ME���v=*4�*%N��֕0:�ޝn�~���};���_��:j(��L��?��[�l����/ 1ޔG ̈́�p�g$c�hUd�����5�ԩ���滗IDB@   �e }3T�m��.@;���֭B�$O��%0���(�ZI6����}Er�腡 AVe�1�s�ǧC�1#�m��Y�nM�A���WbH#.�\�����֙4�6��#�;������m�w,Z��)��p9�8���jz�f5P���J�IK�/�-��c����$��&��J��v��SՓk�q4��X�7F�4�gX����T��>�����[ȧa�J��R��)PФ���*�9h;2k�VR�Y�� ϶*�Β�Q���!G<`���S��<�*�P:�G�_Ӛlw%ʰA�����GX����9+�6KY�J����"u���(�B�"�yA���9�Iop����eO�z�lg2��"pRG:ђ:Uy""��4WKa&G#�a� V �賂�O�����̆J���ʘ��+�;�%jVer(�����qԲ��E�D��F+6z�c�I9.    ���)d��+E?;O�a�8��WY�m��E�_m��$�n�s������IG���Us��=�y�3����΍UZ�z4B2K�J͌/�:}i]fr���=03'\w���m��p�2|��<���5,��.#(���-��K�ٚ���gO�x�������Mv%�d�c�@'v}=��N{k[�kF�i�y%Y%���~uGːFm���7�"1�zZ(�n���l��P��$k;�H�n�����[��I;}�ݭ��|��zdbS�ZHQ~PX�{�����Cp�,�;�$e����qBM6�5��.�"�e���U����C�pS��E[�y(G�"�A��|U=��5���JHb�F��c����b���H"�6_,d0����`�b�ؼ�EZӭ��;�{��#(�z�G?�K6����,<�e���=*9���'�\f�bYa�7Nꌹ�A���lSU���m��;��Է�pT�v��9�UmB����n��#��-;!�������#��#ڹ�J܆/����,�Ep���e�:�S�Y��B�i�Xm]X�gN�Iֵ��k:U��~����(r�� )�(��&Qԝ*�u]j�u�=:��X1t@���x�V֒>�$�тȁT�� *��7��$��4��|1*�    �͎�?�Z��5%ҧk�P �<�֪Z�r��B������}�!T�sF�4{�����*�e+�H�II4�6u]k�ȇ1��N�xA�*��0����UC`�u?�V��ꕃ�*7`���Ҡ��od1F�1�����Z��  <�2�,�nX� ��7�:d��#,�w�e�����J�t�ʿ���kM�ۨ�EX���^rA�y⡚7�/p���Ѓߊ�-����6ljЙw��}* ������R$EP@#����k+j�J�[��?��,bs�]��ܒ3^�}�4����%�N�01���+ƅ��v�Ll�(@X��j����E���<� �O�:iQ�����n�2�i�΃�OQ�#�G)A��'!2a6��ќp:Z�|C�-��p6;c���Z共��27��Z� 7�[t:pʢ�����;װk�T�"��鰎�֩^_Gv�P�VfWyD��U>���r����A��]T(���S]�<3}k2^�zS�� �v����c3*0�����,��֬�UTՈ��A�\N������|�>k��������ҡYt����$~?�c^v6V���PZ\����d���0��~������Yf3��c�z�g�U�!w���Lq��u�X!�����e$�;�+�w7b�iL�� ����j��y2�HŔ�T��S���M�M��Xʸ<�ydu� ����,�-��9PcauX�;>����͊��m�"[F<��R��O�_J�<�B�����$6w(8��j�1�v#����vzg8�S�X;٥��  ��a�/���IE�Ù-����eB 'v0�|�ƧHJM7 ��D�G��Ԉ��@�c2۱�ñ��ZΩt!��<��� g��=��7!�M{�<����L�\��>c��@�U�+�g�cP�n��3�$��������S^��w&���;�<�u���Tr��$�BCl�)��r"e�s����U������{�(�y�� ����1����i�,���#��v�Đ<����n�U�#���֢j�]49#�.�#*���Zx�Yf�E�"I�n��ڢV�������$��������[�&H��  ��A�v�W�P�e&H�%�d����My�]o1��@l�1ӯ��騾M�3N����c�jT�9�M���FA�6�����țrB�|��8\�ߥs:��L?.�^�6�����6U�y�8���@*�=:���i=I��3��� � "�Iֽ��yX��QJ�V�����b3U��CJF�e��WE�L�b?o��s5miS&���CG?���d9����N�锘�"+K)ʎ1��zTI,�p���О���$M�YY1c��g���Y�L�~F@�g�$�;  d�8%�"��&Y��3��z�>�82�1���!��HN���dEC��p��=���Q���9>Z�Pv��$��>�:5�$��bo?�R��!�9�Ǩ=��1u4�8t!$^\y==x��8�>b    bS+�{��J����s���īl?���P�m��Q&\:���8�⡑ƞ[<(Y�X��L�(yH��>�����Yw3��=���B�a�v��Ms"  �awd�z��޲�B���hfb�����Ϧi��ɔ��b@RF01���Q-�o*30E�1�-���1M;��N�$�b�#���s�c�������,p,��&M�BH��P]�S������c�i�I���L��ljpF=x�����@�ƚ#3���x���ʴ/��\i�B  @�����#�ګ^��g*N�S�ګ�̒8�R�ǵkd��k�HV�pd ��`c4�h���wg��S뎔�B$ܒ�*(-׀ �Җ�s)v��>E����M=|�@҈�ۖ;���c���~����Rq�WC1�j2�FrGA���e��v�&�pkV�EisT�zJ�-z���1H��R��<�nD��CU���:��D��ǯjlΓoT�Q��T���5�sW1ޱ���=�4���d�iH�1V8_Zpx�F!/ʋ�ϧ�di�"Ehdp����j�82���*��{zׇR��|����+�W���<�6�'$w�_֤�$�A� 2��wv<��-���ꊒ�\(���2�m�b̀xON `����g2*�s�΄�����N}�m��!��a������5�ĩ�Yc9�o9E'���b���@��3�z��*�0#/���sX����J{�y`p� �f�S��RGd���͹Cnj��4�w.�   h���oS�����MA��Z��wS[\1�q,��@8:�x'��$A���ˌ�{�SSZ�o.�c:�^1� Ѷ�eiX#7�Τ63���5\ʖ���� �#�)n7:`����jQM�a̱0 K�H�O�[p�R��{��U%����)�iM�$�^���S������k9�U}~�%^��,ev`��2J���WO��|Ĝ�>�w�T�Oh�) +�#���c�ƙon򕁘�&�    ���S:'��7`�T���w=i�l�S[�<���1_((۱s��֩[��"ƥ����>��M"2��b��@��V-c��]���^F�ps�?.?Z�+-�3��x� y-��w�?�Q��d�ӎp8���MQ��E*��2���|VN��e@=�jSWI�\����Jj��J^��Gc��2�u��������"�Ii'��v�+3D�a�5f���a��Im�����n��~J�Ei��Ż�a�������R#���������;V-��:Zŋx�8����Kӝ]yt`    ��n�r1I�үY]%�r����]R7[�Ҥ�-������)�<�O�!�ԏ| [��cL��FR0�ۥ�� �ݴt�?���yq��; m��H=;���+Kݠ�l�z���f��s�>�:�r!qTxQ�O�s�����8�2DC(�s��)@���J� ���{������ItA�M�(���$��sUa��bC�9�;�ǽXG=����g�#�;c�~5n��"�pKw'|h��GOj�nU��)��$w$wp n��^���1yp�#�V=:����O.��a�9��[ֵ�Y���+� ��Y��5ڽ�L])և$]��j�;�n`��W ��=�y����x���P��is�5��N�`���3���:���k����,�ҵu*֚�W�A��R�/q|�p�J�Pl���9�p?J�!��d��Z�`#�G`?¬�l�,R�"�!�Q�h00$c�"�o���K��WFj��i�*��O�ޭiڒ�H$X��,��v���j)�+�Y�ڣ���)��ܼ�  ���b���j�׆d�-��c��z��/�`�pI�*���I��c�$�ϭvP��ni:�(dҊ�#f�#櫹�V(�15���4SI��<��*>�"���F8 ��.E��e�ƴ-�p�$�VP��W��S;����wL���"G������!���ås�\2p�Jӳ�%��`���d¹�Ni�'�a� s\��ֆ�tu-� 0:V�\3���kd���zW?���M�_�>=ZX�����ʸ����Q�;��I�s\�l[���*�4a��d��{t�Z�!��}��1\����#�`���OC���>��{6п�W���Ey5�ΛwV!���R��,q#�r�w}�z����"������9�}Ej��t$)I�X�O�]]69H�r�)����k�ڴ�f���G=�B�e[� ����^��`|�byw^B�L}j��dܟ�R����>��H4�@��b�w��R�}�Ջ[�f�D�s�ێ�F['���H2F�ZF�)�_bq��v���#�m�o�PJ�$9��#;�R���Л��'�U\��<�!p�2q�ͼ�E��M������U�MTG,��rF��� �e���c�=k��a��l[ij��2���ۜ�^�����j&�=��޽jT]�z�▃�J��=���5t�y�k��Bƕ��Mh��T�ƓE4�*�f�����E3�� R6���V��L��d��T�SE p��RS�h[�w0Ʊ�#"�)�O��Vs�d��s��I.����.�*�z�Ҵ��Y����� �d�Q^}l�)���-D)ò�2�Nrd�~��(`��x�Ɗ+8���bٙs��O�+��Z̖�YX�����(��ti��G-J�[233�����$�i�Mjș�Q&�*�9Ɍ-Q�EH��c �Eg<��EPd���


A var_dump on data gives basically the same thing for thousands of lines on the site:

string(123362) "GIF89a������!)19J��������������������������!�!�!�!�!�!�)�)�)�)�)�1�1�1�1�9�9�9�B�B�J�R�Z�c�k�s�������޽��c��1��!���{��{��Z��J��9��)��!�����޵�֥�ƌ��s��R��B��1��)����������Δ�Ƅ��9��1��!�����ޭ�΄��{��R��B����J��9�ޥ�֌��k��������{��s���������19Bs{����JR����Zck���������������,��@� H�@�8q (h�@�"lx�D���Сŏ�I8r�NJ&㘄�1���!E�,`����8Sތ3g�J�rz�왳蜟s��,:�hS�$��<��S�D�NM���V�N�^e���T�9�|��v�ڶn��Mjq��u������˽[�x�pE�=N���Lj-=0��䅔R0���A `4hͦ?�<�t��\�M��l�j����ܾy��-@Rǖ�*������JG����#k������e���Cໜ�?-��y5΁�f��9{3$�9��uj�(]����z�u�U�y�VW�)�_\�$a��%��Hj���[i��Y<]��X�a`Sf���R%�՗M,���K%�$�z�dBHݏE6�F�U���-$�o���Yg���i���䑶���l��v�o[��� �en�X䏃-�\r,I��c'���F�4I|n�RL�!�ލ!j�{6Q�Q/���I �{O�G`�Vu�aSY�'�ZN(a�V�|&�W`�"���|2��֪���j[�J����k��%`|*����6x!{q-�3���x*�g���y�fGY榛qP��@��%t�i�k�F�����[����֤��n�o��rY�o��ɛq�f$.�'�{tU�'G4Ť�76�w��4�u��t����a��.{_�a�Z�ʽ�묺���|o��\�+ ����j��+� ���2']t�>��H����@���S?���4_H��s�ڠJ�����)���J�)D�����B}�\���Pf�E�)�gVNIo���ko�a�[ۘ����c���0M�)��t�]��Kn�hRQ��M�UF]��Wj�r�����ϫ����C���T�HѻC��B-������C'���H_] ���<�W����G�����<�Pg?���#�����/>�������;�3�L�h��6�'R�!�Qcp{�4"��d�`�қhR%���]}�R�^C�z�k5^ڗo��xbA�2�9*&�YQ�,�B��%@��T��!�mg���Өf��Aw=��������I��ǻ^��w>�)1���"����'���XD ��E,��]̢�0*��T�"�p=:�qd��8�-�Q�׻��H�$�ωK������ (׹�����(�1�Y`A$��w��o�r����~}�^[rMs9�$�:-9["ϣ�j��&3JP��ZMeh� �������{����W<"�o�Sf2�xEb:��X|���;汚u�#6��M6�������)G7���朣9��Mq�����C� �nΓ����>�Y�O{���t\#��E1:1����!���Nr8�s$b0D2�h���]�H��`���(ŦKY:\q��H�P=<ʓOPh����,���d!�\�UR�/�v�Y �|�l�����<���-��(U�jq��̪;�Y�rs���*X�i�yڡ���Z�zO����p}+[��V>ԁ|�^��W���}�k��v�~��������=�`�X�浭n-k<��F�F�}���j�"�ܥD�J  u��0�����[S�ji5�I�(x�y�0�JHL�+�V"�<�)�r_x3A�*fF����(��}��(ը*э��*5�HNm�1�] (A�Iϴ���d��]�*��N6���CckX?H�~`�����.��N���7��� �+���p�� ? ��GL�[�8��W��?������X�구l�g<��G-:q������:�ve<�Y ��#�I��e�Է��\�M�%�V��K�Mgz3��''�AOupZ�����dNs�����fz7�ټ�Ùg��w��L'��� b��o~�:W��S�sc�hD��C"K�q�S��x�2R�A虂���N�h~�yq��8�%��àd ����^��(�i�1��,�6�i:❇�6+��zVvC���o}����"������do�ڢ�y�w0�� �9�� T��T;6(v���sm�z��F��=}�h����f��{-�O7����� t��m�`jOu�m-kO���.�;���������6��U�� X�N��]�׵z�ߌ㝡���e�!��֕����N�T�L_����_Ř�t�k����>'b�Wi'`��fɆj>7jߦlBl�fi�l�fm��v5�k�vl��kd'k�6w��k6�i�kh}���s3�|�uc��V��Y�tU�FU��FU$d�D;�r!�B?A-"9��0����$�&�I �Q�8h�(pq{ʢC�s>b䃝^��U:�h��r�7_Us��`��`�Vn}t�/�a    ��i��t�V~@J0�ht7�DWtE�:���ij5�����+Ƈ�Hv�t�VjCPk��~>�m��t��k"��w�l�f�C �XtD�GGt�xtfgk�uw�t��u�v�8�ӆl�6m8��v��t��k8m�6cphs/�r8V|��c`V��GbhE��<,�r!?!)���Qw$�S��@r�$��eQ���,W+>�5�3=P��No�4h��ho5O�GY�VYvX��s;Gns8}x�a-�a �a f���t�6����ؒFWt:`w��kCG�u��F��vЈw�V��i9�j�t��s�&b�&���CЌ��iK����u��uF7��v�I�vCpH�p����u-��C����s_wwlW��H�v+iw�6j��j_�tG��:�5�w�k��~ �a &iٸ��Ic��W��c6�U��G��C�s;�R#+�"�S(�s-%pR�&O7�4&#:C4EE�EcN�y���V�r�5Yp�`0�a�a Ynf'����|H��8��Ƈ+��R�~��uE��(��8�A0�N��k�֝9���&�6��6tD�v�w'��m爣6k'pl�6�@ ���s���:l]7l1@t}�t�ɝ90^v�����+�w���fi�8d����tȈj���9@ W!��:`i�j����l��v�i�G���`qh���~��x5��u�^%P�dUu=�H*Sq(�d�649�Q����dI�c+V�מ)mG


A var_dump on $image right after curl_close() gives:

resource(65) of type (gd)


Also if I try using imagejpeg() before all the resizing code, it still gives basically the same thing. I'm stuck :/



Even with Musa's fix of setting the content header, it still just displays the raw image data:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www...../image31.jpg'); //Actually full URL in code
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); 
$data = curl_exec($ch);

curl_close($ch);

$image = imagecreatefromstring($data);
//var_dump($image);
$thumb_width = 200;
$thumb_height = 150;

$width = imagesx($image);
$height = imagesy($image);

$original_aspect = $width / $height;
$thumb_aspect = $thumb_width / $thumb_height;

if ( $original_aspect >= $thumb_aspect )
{
// If image is wider than thumbnail (in aspect ratio sense)
$new_height = $thumb_height;
$new_width = $width / ($height / $thumb_height);
}
else
{
// If the thumbnail is wider than the image
$new_width = $thumb_width;
$new_height = $height / ($width / $thumb_width);
}

$thumb = imagecreatetruecolor( $thumb_width, $thumb_height );

// Resize and crop
imagecopyresampled($thumb,
$image,
0 - ($new_width - $thumb_width) / 2, // Center the image horizontally
0 - ($new_height - $thumb_height) / 2, // Center the image vertically
0, 0,
$new_width, $new_height,
$width, $height);

header('Content-Type: image/jpg');
imagejpeg($thumb);


Tried using a jpeg to start with just to see if it changed anything, but still had the same result, gibberish.




After getting the image to display I tried making a function for this code in my header:

function resizeJpeg($url, $thumb_width, $thumb_height)
{
echo "URL: " . $url . "<br />";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); 
$data = curl_exec($ch);

curl_close($ch);

$image = imagecreatefromstring($data);

$width = imagesx($image);
$height = imagesy($image);

$original_aspect = $width / $height;
$thumb_aspect = $thumb_width / $thumb_height;

if ( $original_aspect >= $thumb_aspect )
{
// If image is wider than thumbnail (in aspect ratio sense)
$new_height = $thumb_height;
$new_width = $width / ($height / $thumb_height);
}
else
{
// If the thumbnail is wider than the image
$new_width = $thumb_width;
$new_height = $height / ($width / $thumb_width);
}

$thumb = imagecreatetruecolor( $thumb_width, $thumb_height );

// Resize and crop
imagecopyresampled($thumb,
$image,
0 - ($new_width - $thumb_width) / 2, // Center the image horizontally
0 - ($new_height - $thumb_height) / 2, // Center the image vertically
0, 0,
$new_width, $new_height,
$width, $height);

header('Content-Type: image/jpeg');
return $thumb;
// Also tried 'return imagejpeg($thumb);' and simply 'imagejpeg($thumb);'
}

And tried using it in this way:

$imageData = resizeJpeg('http://upload.wikimedia.org/wikipedia/commons/1/1e/Stonehenge.jpg', 150, 150);
header('Content-Type: image/jpeg');
$imageSrc = "<img src = '" . imagejpeg($imageData) . "' />";

Does it have to be in file or is there a way to make it see it as an URL?

9
  • What's the problem? It seems that you are getting image output just fine. Commented Sep 9, 2012 at 20:03
  • It's displaying that image data all over the page instead of the actual image. See above. Commented Sep 9, 2012 at 20:12
  • is this the whole file, nothing but the file? Commented Sep 9, 2012 at 20:18
  • 1
    @iRector, You're not trying to directly include this in a page, are you? You need to access this like you would any other image. <img src="yourscript.php" /> Commented Sep 9, 2012 at 20:20
  • works for me copied whole script above, get nice pic of sun Commented Sep 9, 2012 at 20:23

1 Answer 1

1

You have to set the mime type for the browser to render the data as an image, this can by done by setting the content type header before you output the image.

header('Content-Type: image/jpeg');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.