Forum Moderators: coopster

Message Too Old, No Replies

Resize and Save Image?

         

PartisanEntity

9:12 am on Feb 5, 2010 (gmt 0)

10+ Year Member



Hi there,

I have some PHP code which which I am generating a jpg image from a Flash movie.

I would like to resize it, lets say by 50% and save it. But I am not quite sure how to do that and would really appreciate any tips or ideas.

Here is what I have so far:

<?php
$data = explode(",", $_POST['img']);
$width = $_POST['width'];
$height = $_POST['height'];
$image=imagecreatetruecolor( $width ,$height );
$background = imagecolorallocate( $image ,0 , 0 , 0 );
//Copy pixels
$i = 0;
for($x=0; $x<=$width; $x++){
for($y=0; $y<=$height; $y++){
$int = hexdec($data[$i++]);
$color = ImageColorAllocate ($image, 0xFF & ($int >> 0x10), 0xFF & ($int >> 0x8), 0xFF & $int);
imagesetpixel ( $image , $x , $y , $color );
}
}
//Output image and clean
header( "Content-type: image/jpeg" );
ImageJPEG( $image );
imagedestroy( $image );
?>

chrisranjana

9:23 am on Feb 5, 2010 (gmt 0)

10+ Year Member



imagecopyresampled should do the trick

more info here

[edited by: eelixduppy at 10:01 pm (utc) on Feb 5, 2010]
[edit reason] removed URL [/edit]

PartisanEntity

9:58 am on Feb 5, 2010 (gmt 0)

10+ Year Member



Hey thanks for the quick reply.

 imagecopyresampled( $img_dst, $img_src, 0, 0, 0, 0, $iw, $ih, $is[0], $is[1] );


for $img_dst is it possible to use a string like "../images/car.jpeg" or am I completely missing the point?

chrisranjana

10:13 am on Feb 5, 2010 (gmt 0)

10+ Year Member



I guess those 2 should contain image handles

$img_src = imagecreatefromjpeg( $sourcefile );
$img_dst = imagecreatetruecolor( $iw, $ih );

PartisanEntity

10:29 am on Feb 5, 2010 (gmt 0)

10+ Year Member



Okay this is what I have sofar, the image is generated and saved.

But I am missing a step inbetween that resizes the image before it is saved, and I could not get my head around how imagecopyresampled works?

<?php
//get the session id from the flash movie
session_id($_POST['myID']);
session_start();

$data = explode(",", $_POST['img']);
$width = $_POST['width'];
$height = $_POST['height'];
$image=imagecreatetruecolor( $width ,$height );
$background = imagecolorallocate( $image ,0 , 0 , 0 );
//Copy pixels
$i = 0;
for($x=0; $x<=$width; $x++){
for($y=0; $y<=$height; $y++){
$int = hexdec($data[$i++]);
$color = ImageColorAllocate ($image, 0xFF & ($int >> 0x10), 0xFF & ($int >> 0x8), 0xFF & $int);
imagesetpixel ( $image , $x , $y , $color );
}
}


$today = date("Ymdgi");

//Output image and clean
$save = '../images/cars/'.$today.'_'.$_SESSION['username'].'.jpg';

header( "Content-type: image/jpeg" );
ImageJPEG( $image, $save, 100 );

imagedestroy( $image );


?>

chrisranjana

11:01 am on Feb 5, 2010 (gmt 0)

10+ Year Member



Just thinking aloud

$img_dst = imagecreatetruecolor( $iw, $ih );

$iw and $ih are the 50% reduced dimensions

and

$img_src is the $image

PartisanEntity

11:54 am on Feb 5, 2010 (gmt 0)

10+ Year Member



I have modified my code now.

I generate an image from Flash and save it.

Then I grab the saved image, copy it, resize it and save it.

BUT: the resized image is completely black?


<?php
//get the session id from the flash movie
session_id($_POST['myID']);
session_start();

$data = explode(",", $_POST['img']);
$width = $_POST['width'];
$height = $_POST['height'];
$image=imagecreatetruecolor( $width ,$height );
$background = imagecolorallocate( $image ,0 , 0 , 0 );
//Copy pixels
$i = 0;
for($x=0; $x<=$width; $x++){
for($y=0; $y<=$height; $y++){
$int = hexdec($data[$i++]);
$color = ImageColorAllocate ($image, 0xFF & ($int >> 0x10), 0xFF & ($int >> 0x8), 0xFF & $int);
imagesetpixel ( $image , $x , $y , $color );
}
}

//generate todays date and time for the filename
$today = date("Ymdgi");

//combine date and username
$filename = $today.'_'.$_SESSION['username'];

//Output image and save it with the filename we generated
$save = '../images/cars/'.$filename.'.jpeg';
header( "Content-type: image/jpeg" );
ImageJPEG( $image, $save, 100 );
//clean up
imagedestroy( $image );

$thumb = '../images/cars/'.$filename.'.jpeg';

$new_thumb = imagecreatefromjpeg($thumb);

//list the width and height and keep the height ratio.

list($srcwidth, $srcheight) = getimagesize($thumb);

//calculate the new size

$newwidth = $srcwidth / 2;

$newheight = $srcheight / 2;

$new_thumb = imagecreatetruecolor($newwidth,$newheight);

//the resizing is going on here!

imagecopyresized($new_thumb, $thumb, 0, 0, 0, 0, $newwidth, $newheight, $srcwidth, $srcheight);

$thumbsave = '../images/cars/thumbs/'.$filename.'.jpeg';

header( "Content-type: image/jpeg" );
ImageJPEG( $new_thumb, $thumbsave, 100 );
//clean up
imagedestroy( $new_thumb );

?>

chrisranjana

12:31 pm on Feb 5, 2010 (gmt 0)

10+ Year Member



Hopefully

[mummey.org...]

should solve your problem.

PartisanEntity

12:35 pm on Feb 5, 2010 (gmt 0)

10+ Year Member



Thank you, actually I am not having any issues with transparencies since the large images are saving fine.

It is just the thumbnails that are completely black and I am still not quite sure why.

miguelvidal

1:29 pm on Feb 5, 2010 (gmt 0)

10+ Year Member



Olá!
Hello!

Este é o meu código, funcionando e testado:
This is my 'working and tested' code:


/**
*
* Redimensionar e salvar uma imagem
* Resize and save an image
*
* @author Miguel Koscianski Vidal - miguelvidal@siscompar.com.br
* @since 2010-02-01
*
* Seja Honesto! - Be honest!
*
* Ao utilizar um código criado por terceiros, mantenha os créditos do autor original.
* Ao modificar um código criado por terceiros, mantenha os créditos do autor do código adicionando as informações e créditos relativas à sua modificação.
*
* If you use any code made by another person, don't remove the author's information.
* If you modify any code made by another person, don't remove the author's information and add yours.
*
* Respeite seu idioma e sua nação! - Respect your language and your nation!
*
* Ao produzir qualquer material, sempre que possível faça-o em sua língua materna respeitando a língua culta e correta (sem erros!).
* Adicione tradução para todos os idiomas que quiser, mas não remova o original que deveria ser feito em sua língua materna.
*
* When producing any material, whenever possible make it in your native language respecting the cultured and correct language rules (without errors!).
* Add translation for all languages you want, but do not remove the original that should be done in your native language.
*
*/
$imagem = "/caminho/da/imagem.jpg" // deve ser JPG - must be JPG
$nome = "minha_imagem.jpg" // nome que será salvo - name to save
$destino = "/onde/salvar/" // atenção na '/' final - notice the final '/'
{

// largura e altura máximos - max width and height
$lMax = 150;
$aMax = 150;

// valores que serão usados na nova imagem - values used in the nwe image
$largura = 0;
$altura = 0;

$novaImagem = imagecreatefromjpeg( "{$imagem}" ); // newImg = create!
// largura e altura originais (da imagem) - original width and height (of the image)
$lOri = imagesx( $novaImagem );
$aOri = imagesy( $novaImagem );
$largura = $lOri;
$altura = $aOri;
//redimensionando... - resizing...
if( $lOri > $lMax || $aOri > $aMax )
{
$largura = $lMax;
$altura = ( $largura / $lOri ) * $aOri;
if( $altura > $aMax )
{
$altura = $aMax;
$largura = ( $altura / $aOri ) * $lOri;
}
}
// criando e salvando a imagem - creating and saving the image
$tmpImagem = imagecreatetruecolor( $largura, $altura );
imagecopyresampled( $tmpImagem, $novaImagem, 0, 0, 0, 0, $largura,
$altura, $lOri, $aOri );
imagejpeg( $tmpImagem, "{$destino}{$nome}" );
$tmpImagem = imagecreatetruecolor( $largura, $altura );
imagecopyresized( $tmpImagem, $novaImagem, 0, 0, 0, 0, $largura,
$altura, $lOri, $aOri );
imagejpeg( $tmpImagem, "{$destino}{$nome}" );
}