Convert Jpg /PNG To GIF

drama22
drama22 used Ask the Experts™
on
i have this idhttp code that download images to memory stream and save it on disk

i want to convert jpeg images and png to GIF then save them to disk

if (itsimage = 'image/jpeg') or (itsimage = 'image/png') or (itsimage = 'image/gif') then
begin
try
aIdHttp.Get(trim(FURL), aMs);
except
end;
aMs.SaveToFile(path);
end;

try
if aIdHttp.Connected then
aIdHttp.Disconnect;

except // do not care about any errors yet, maybe log them later

end;

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
If you really need to do it in Delphi, the best for you would be to ask at some Delphi forum. The reason is that it has some specialized libraries/classes, so there is a kind of native-for-Delphi way to do it. The following link is just example

http://www.delphipages.com/forum/showthread.php?t=173904

If you need more complex image transformations, then try ImageMagick -- free software product (http://www.imagemagick.org/) that gives you both ready to be used utilities (executables) to do the transformations from command line, and also libraries that can be linked to your program in many programming languages, Pascal being one of them (from the page):
Choose from these interfaces: G2F (Ada), MagickCore (C), MagickWand (C), ChMagick (Ch), ImageMagickObject (COM+), Magick++ (C++), JMagick (Java), L-Magick (Lisp), Lua (LuaJIT), NMagick (Neko/haXe), Magick.NET (.NET), PascalMagick (Pascal), PerlMagick (Perl), MagickWand for PHP (PHP), IMagick (PHP), PythonMagick (Python), RMagick (Ruby), or TclMagick (Tcl/TK). With a language interface, use ImageMagick to modify or create images dynamically and automagically.
Software architect
Top Expert 2012
Commented:
When you detect which type is ...then use convert functions:
uses Jpeg, GifImg, PNGImage;

function JpgToGif(ms: TMemoryStream): Boolean;
var
  gif: TGIFImage;
  jpg: TJPEGImage;
begin
  Result := False;

  gif := TGIFImage.Create;
  try
    jpg := TJPEGImage.Create;
    try
      //jpg
      ms.Position := 0;
      jpg.LoadFromStream(ms);
      jpg.DIBNeeded;
      gif.Assign(jpg);

      //save...
      ms.Clear;
      gif.SaveToStream(ms);
      Result := True;
    finally
      jpg.Free;
    end;
  finally
    gif.Free;
  end;
end;

function PngToGif(ms: TMemoryStream): Boolean;
var
  gif: TGIFImage;
  png: TPNGObject;
begin
  Result := False;

  gif := TGIFImage.Create;
  try
    png := TPNGObject.Create;
    try
      //jpg
      ms.Position := 0;
      png.LoadFromStream(ms);
      gif.Transparent := png.Transparent;
      gif.Assign(png);

      //save...
      ms.Clear;
      gif.SaveToStream(ms);
      Result := True;
    finally
      png.Free;
    end;
  finally
    gif.Free;
  end;
end;

Open in new window


PngImage can be downloaded from here...
Note : ms will be overwritten - and ready to save to disk ....
if PngToGif(ms) then
      ms.SaveToFile('....test.gif');

Open in new window

Author

Commented:
i tried

function JpgToGif(ms: TMemoryStream): Boolean;
var
  gif: TGIFImage;
  jpg: TJPEGImage;
begin
  Result := False;

  gif := TGIFImage.Create;
  try
    jpg := TJPEGImage.Create;
    try
      //jpg
      ms.Position := 0;
      jpg.LoadFromStream(ms);
      jpg.DIBNeeded;
      gif.Assign(jpg);

      //save...
      ms.Clear;
      gif.SaveToStream(ms);
      Result := True;
    finally
      jpg.Free;
    end;
  finally
    gif.Free;
  end;
end;

Open in new window



if (itsimage = 'image/gif') then
begin
try
aIdHttp.Get(trim(FURL), aMs);
except
end;
aMs.SaveToFile(path);
end else if (itsimage = 'image/jpeg') then
begin
try
aIdHttp.Get(trim(FURL), aMs);
except
end;
if JpgToGif(aMs) then
begin
aMs.SaveToFile(path);
end;
end;

Open in new window



it doesn't save any thing ..

Raise this error

jpeg error #53
Sinisa VukSoftware architect
Top Expert 2012

Commented:
So, this is not really a jpeg file as should be... try to save it before conversion... and look for contents with some hex editor....

Author

Commented:
If its not jpeg file header of indy wont read it image its self saved from photoshop as jpg I think there is issue with ms . Because normal gifs saved normally

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial