note 70975 added to function.imagecreatefromstring

From: Date: Tue, 07 Nov 2006 01:22:27 +0000
Subject: note 70975 added to function.imagecreatefromstring
Groups: php.notes 
Request: Send a blank email to php-notes+get-119449@lists.php.net to get a copy of this message
This took me a while to figure out, so hopefully I'll save someone the trouble. There's some kind of nastiness that happens when trying to send an jpg or other image from a c# COM dll to php. I tried a number of different string formats and was unable to get them to talk to each other... something with the strings getting truncated. The solution that I found (although clunky) is to convert the image to a byte array, and then convert each byte in the array to an int. Then pack those ints off into a string with a space between each one. Use explode on the php side and then you can call the all powerful imagecvreatefromstring function to do your bidding. :) C# code: public string DrawImage() { Bitmap b = new Bitmap(@"c:\bob.jpg"); MemoryStream str = new MemoryStream(); b.Save(str, ImageFormat.Jpeg); byte[] buf = str.GetBuffer(); return Convert(buf); } private string Convert(byte[] bytes) { StringBuilder sb = new StringBuilder(); for(int i = 0; i < bytes.Length; i++) { sb.Append(bytes[i].ToString()); sb.Append(" "); } return sb.ToString(); } And on the PHP side: function GetImage() { try { $image = $this->m_comInstance->DrawImage(); $items = explode(" ", $image); $x = ""; foreach($items as $i) $x .= chr($i); return imagecreatefromstring($x); } catch(Exception $e) { print($e); } } -Will Shaver ---- Server IP: 64.71.164.2 Probable Submitter: 70.91.222.57 ---- Manual Page -- http://www.php.net/manual/en/function.imagecreatefromstring.php Edit -- https://master.php.net/note/edit/70975 Del: integrated -- https://master.php.net/note/delete/70975/integrated Del: useless -- https://master.php.net/note/delete/70975/useless Del: bad code -- https://master.php.net/note/delete/70975/bad+code Del: spam -- https://master.php.net/note/delete/70975/spam Del: non-english -- https://master.php.net/note/delete/70975/non-english Del: in docs -- https://master.php.net/note/delete/70975/in+docs Del: other reasons-- https://master.php.net/note/delete/70975 Reject -- https://master.php.net/note/reject/70975 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#119449) next »