Portrety Uliczne Nieznajomych - zobacz wyjątkową galerię portretów z warszawskich ulic
ZALOGUJ SIĘ
login:
hasło:
przypomnij hasło
załóż konto użytkownika
(i zobacz kilka porad gratis)
   
WYSZUKIWARKA I DZIAŁY
całe porady  tytuły
zaznacz działy do przeszukania
(brak wyboru = wszystkie działy)
PHP
MySQL >
PostgreSQL
SQLite
Perl
Java
XML
XSLT
XPath
WML
SVG
RegExp
Wyszukiwarki
Ochrona
VBScript
Google Plus
XHTML/CSS
JavaScript
Grafika
Flash
Photoshop
Windows
Linux
Bash
Apache
Procmail
E-biznes
Explorer
Opera
Firefox
Inne porady
   
KURSY, DOKUMENTACJE
Własne:
XHTML/CSS
JavaScript
ActionScript
WML, RSS, SSI
Pozostałe:
PHP
MySQL
Java API
więcej...
   
użytkowników online: 68
W CZYM MOGĘ POMÓC?


   
OPINIE UŻYTKOWNIKÓW
Na początku, kiedy zobaczyłem, że ktoś chce jakiejś opłaty za pomoc w tworzeniu stron ryknąłem śmiechem - potem przyszły problemy... i zaryzykowałem. Druga rzecz to: nie chciałem "kopiować". Ale prawda jest taka: są lepsi, bardziej doświadczeni i... czasem trzeba poprosić o pomoc, a jak poświęca się na to trzecią cześć życia, to nic dziwnego, że nie chce się swoich "sekretów" zdradzać za darmo. Skorzystałem z "algorytmy.pl" i naprawdę jestem z tego w 100% zadowolony, polecam - dla zawodowców (co się uczą) i amatorów (można skorzystać z gotowego rozwiązania).

Tomasz Czypicki
Cybernoxa

   
GALERIA FOTOGRAFII
   
PODRĘCZNIK PHP 5.x, 4.x, 3.x - częściowo spolszczony / źródło: www.php.net

[Spis] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [X] [W] [Z]

imagecreatefromstring

(PHP 4 >= 4.0.4, PHP 5)

imagecreatefromstring -- Create a new image from the image stream in the string

Opis

resource imagecreatefromstring ( string image )

imagecreatefromstring() returns an image identifier representing the image obtained from the given string. These types will be automatically detected if your build of PHP supports them: JPEG, PNG, GIF, WBMP, and GD2.

Zwracane wartości

An image resource will be returned on success. FALSE is returned if the image type is unsupported, the data is not in a recognised format, or the image is corrupt and cannot be loaded.

Przykłady

Przykład 1. imagecreatefromstring() example

<?php
$data
= 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl'
      
. 'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr'
      
. 'EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r'
      
. '8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==';
$data = base64_decode($data);

$im = imagecreatefromstring($data);
if (
$im !== false) {
  
header('Content-Type: image/png');
  
imagepng($im);
}
else {
   echo
'An error occured.';
}
?>




User Contributed Notes

kakail at aei dot ca
27-Nov-2005 03:07

I found out that in PHP 4.3.2 and 4.3.4 that the 'imagecreatefromstring' function use a lot of memory when working with big image...
Try this piece of code with a big image (like 1Meg or more)
<?php
$loadFile
= "http://bigimage.jpg";//Try with a big image
$im = imagecreatefromstring(file_get_contents($loadFile));
imagejpeg($im);
?>

With a 400k image. I run with a 16M Mem_limit but for a 1.4M image, the mem_limit required jump to more that 32M


alexandrebr at ignorethis dot gmail dot com
29-Aug-2005 02:08

An easy example to help understanding this function...

<?
$loadFile
= "http://static.php.net/images/php.gif";

$im = imagecreatefromstring(file_get_contents($loadFile));
// identical to imagecreatefromgif($loadFile);
imagegif($im);
?>

The function will try to auto-determine file format (jpg, gif, png....), and will return false if fails.


paul at epeeps dot net
26-Jul-2005 04:39

I run a blogging site that allowed users to publish images from their cell phones.  For some reason, the nokia 3220 produces jpegs with extrandeous data before the EOF 0xFFF9

I wrote this class to allow you to fix the images so that they can be used with GD to resize and manipulate.  Without applying the fix both GD and gimp report errors for the file.  If basically keeps eating bytes from the junk part till the file is a valid jpeg.  Here is an example bunk pic written by a nokia 3220 that you cna test it with http://www.shawnrider.com/moblog/cache/0854747001121624103.jpg

/*
Author: Paul Visco
Hompage: http://www.elmwoodstrip.com?u=paul
AIM: paulsidekick
Notes:  The file allows you fix the jpegs created by the Nokia 3220 picture phone so that they can be manipulated using the GD library with PHP.

Usage:  Simply instanitiate the class and then call the fix method for each image.
e.g.
$nokia = new nokia;
$nokia->debug ="y";
$nokia->fix("yourpic.jpg");
*/

class nokia
{
   public $file;
   public $debug = "n";
   public $dir_name = "";
   private $img; //the image created from the string
   private $str;
  
   function fix($file)
   {
      
       //set the file to fix
       $this->file = $file;
  
       //load the file into a string
       $this->str = file_get_contents($this->file);
      
       //test to see if it is a nokia image or if it has been corrupted previously
       if (substr_count($this->str, chr(0x28).chr(0x36).chr(0x28).chr(0x2B)) == 0 || @imagecreatefromstring($this->str))
       {
           if ($this->debug =="y")
           {
               echo "\n<br />".$this->file." is not a corrupted Nokia pic";
           }
          
           return true;
       }
      
       //make a directory for fixed images if it doesn't exist and is specified
       if(!empty($this->dir_name) && !is_dir($this->dir_name))
       {
           @mkdir($this->dir_name, 0777);
       }
      
       //strip out the funk e crap from the file
       $this->eat($this->str);
      
       //write the file back to itself
       file_put_contents($this->dir_name.$this->file, $this->str);

       //return true for fixed
       return true;

   }
  
   function eat()
   {
  
       //check the image
       $this->img = @imagecreatefromstring($this->str);
      
       //if the image doesn't work then keep striping out crap
       while(!$this->img)
       {
           //cut off the bad bytes one by one
           $this->str= substr_replace($this->str, "", -3, -2);
          
           //check the image again
           $this->img = @imagecreatefromstring($this->str);
       } 
      
       if ($this->debug =="y")
       {
           //notify the user it's fixed
           echo "\n<br />Nasty bits eaten!! ".$this->file." is fixed now thanks to p:labs!";
       }
       return true;
   }

}


not given
03-Aug-2003 11:14

A note to the previous question (if you still don't know it :))...

GIF's are 256 colors (or 8 bit), and the resample function needs true color I guess... that's why it works with JPG's and not with GIF's.

Next thing... you take a string, write it to file, open the file (imagecreatefromgif), and delete the file again.

if you do imagecreatefromstring($string) you can skip the temporary file part.


shiehj at yahoo dot com
12-Apr-2003 06:44

Here is the code I did to create a thumbnail image from the database blob field. The trick is to use "imagecreatefromstring()" to create an image file.

Jack Shieh

<?php
require("dbconfig.inc");

$id = $_GET['id'];

if(
$id) {

  
$link = @mysql_connect($host, $user, $password) or die("Could not connect: " . mysql_error());
   @
mysql_select_db($dbname, $link);

  
$query = "select filetype, image from pictures where id = $id";
  
$result = @mysql_query($query);

  
$data = @mysql_result($result,0,"image");
  
$type = @mysql_result($result,0,"filetype");
  
  
Header( "Content-type: $type");   
  
  
$size = 150// new image width
  
$src = imagecreatefromstring($data);
  
$width = imagesx($src);
  
$height = imagesy($src);
  
$aspect_ratio = $height/$width;

   if (
$width <= $size) {
    
$new_w = $width;
    
$new_h = $height;
   } else {
    
$new_w = $size;
    
$new_h = abs($new_w * $aspect_ratio);
   }

  
$img = imagecreatetruecolor($new_w,$new_h);
  
imagecopyresized($img,$src,0,0,0,0,$new_w,$new_h,$width,$height);
 
  
// determine image type and send it to the client   
  
if ($type == "image/pjpeg") {   
    
imagejpeg($img);
   } else if (
$type == "image/x-png") {
    
imagepng($img);
   } else if (
$type == "image/gif") {
    
imagegif($img);
   }
  
imagedestroy($img);
  
mysql_close($link);
};
?>


adrian_schmidt at yahoo dot com
06-Mar-2003 01:04

I'm trying to get the imagecreatefromstring to work with GIFs. Of course, it won't.
I've read the tips but can't get them to work either.
The following is what I tried, based on above tips:

---

   header('Content-Type: image/gif');
   header('Content-Disposition: inline; filename=file.gif');

   $temp = tmpfile();
   fwrite($temp, $line['image']);
   $src_img = imagecreatefromgif($temp);
   fclose($temp); // this removes the file
   $dst_img = imagecreatetruecolor(100, 100);
   imagecopyresampled($dst_img, $src_img, 0,0,0,0, 100,100, imagesx($src_img), imagesy($src_img));
  
   imagegif($dst_img);

---

where $line['image'] is the gif as taken from my MySQL database...

If anyone that has been able to make something like this work could give me a working piece of code I'd be really greatful!

I would be great if the tempfile could be excluded too...

Below is a working piece of code for jpeg:

---

   header('Content-Type: image/jpeg');
   header('Content-Disposition: inline; filename=file.jpg');
  
   $src_img = imagecreatefromstring($line['image']);
   $dst_img = imagecreatetruecolor(100, 100);
   imagecopyresampled($dst_img, $src_img, 0,0,0,0, 100,100, imagesx($src_img), imagesy($src_img));
  
   imagejpeg($dst_img);


 

 
  © 1996-2012 & Reporter.plmiejscao serwisieabonamentwarunki korzystaniaRSSkontakt