Connecting Tech Pros Worldwide Forums | Help | Site Map

Image Resize & Rotation

Member
 
Join Date: Mar 2008
Posts: 34
#1: Mar 5 '08
Image Resize & Rotation

Hi

I have 2 scripts, one for Image rotation and other image resize and they both are working.

Image resize scripts load the picture and resize it and Image rotation rotate the image by 90 deg. They are two differennt files i.e. resize.php and rotate.php.

What I want to do is to combine both rotate.php & resize.php files, so when the script resized the image than it will call rotate script to rotate the image and display it on the screen.. I hope I am making sence.. I am finding hard to explain.. If u dont understand anything please let me know..
*************************
Code for rotate.php
[php]<?php
// File and rotation
$filename = 'image.png';
$degrees = 18;

// Content type
header('Content-type: image/png');

// Load
$source = imagecreatefrompng($filename);

// Rotate
$rotate = imagerotate($source, $degrees, 0);

// Output
imagepng($rotate);
?>
============================
Code for resize.php
<?php
$src_img = imagecreatefrompng('image.png');
$srcsize = getimagesize('image.png');
$dest_x = 200;
$dest_y = (200 / $srcsize[0]) * $srcsize[1];
$dst_img = imagecreatetruecolor($dest_x, $dest_y);
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0,
$dest_x, $dest_y, $srcsize[0], $srcsize[1]);
header("content-type: image/png");
imagepng($dst_img);
imagedestroy($src_img);
imagedestroy($dst_img);
?>[/php]thanks

Please enclose any code within the proper code tags. See the Posting Guidelines on how to do that.

moderator

hsriat's Avatar
Expert
 
Join Date: Jan 2008
Location: Bath, UK
Posts: 1,609
#2: Mar 5 '08

re: Image Resize & Rotation


Don't output $rotate and use it as:
$src_img = $rotate;
Member
 
Join Date: Mar 2008
Posts: 34
#3: Mar 7 '08

re: Image Resize & Rotation


Quote:

Originally Posted by hsriat

Don't output $rotate and use it as:
$src_img = $rotate;

well i changed the output "$src_img = $rotate;" but it still did not work... Just to let know there are two different files

File 1 is reseize.php

File 2 is rotate.php

thanks
hsriat's Avatar
Expert
 
Join Date: Jan 2008
Location: Bath, UK
Posts: 1,609
#4: Mar 7 '08

re: Image Resize & Rotation


[php]<?php
$src_img = imagecreatefrompng('image.png');
$srcsize = getimagesize('image.png');
$dest_x = 200;
$dest_y = (200 / $srcsize[0]) * $srcsize[1];
$dst_img = imagecreatetruecolor($dest_x, $dest_y);
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $dest_x, $dest_y, $srcsize[0], $srcsize[1]);
imagedestroy($src_img);

//header("content-type: image/png");
//imagepng($dst_img);
//?><?php
// File and rotation
//$filename = 'image.png';
$degrees = 18;

// Load
//$source = imagecreatefrompng($filename);

// Rotate
$rotate = imagerotate($dst_img, $degrees, 0); //CHANGED
imagedestroy($dst_img);

// Content type
header('Content-type: image/png');

// Output
imagepng($rotate);
imagedestroy($rotate);
?>[/php]
See if this works.
Member
 
Join Date: Mar 2008
Posts: 34
#5: Mar 7 '08

re: Image Resize & Rotation


Quote:

Originally Posted by hsriat

[php]<?php
$src_img = imagecreatefrompng('image.png');
$srcsize = getimagesize('image.png');
$dest_x = 200;
$dest_y = (200 / $srcsize[0]) * $srcsize[1];
$dst_img = imagecreatetruecolor($dest_x, $dest_y);
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $dest_x, $dest_y, $srcsize[0], $srcsize[1]);
imagedestroy($src_img);

//header("content-type: image/png");
//imagepng($dst_img);
//?><?php
// File and rotation
//$filename = 'image.png';
$degrees = 18;

// Load
//$source = imagecreatefrompng($filename);

// Rotate
$rotate = imagerotate($dst_img, $degrees, 0); //CHANGED
imagedestroy($dst_img);

// Content type
header('Content-type: image/png');

// Output
imagepng($rotate);
imagedestroy($rotate);
?>[/php]
See if this works.


A BIG Thanks to You.. I was working on this from last week.. Thanks very much.. Just one more question.. If I want to resize and rotate .jpg file, so I just replaced .png with .jpg, gif and so on.

Thanks
hsriat's Avatar
Expert
 
Join Date: Jan 2008
Location: Bath, UK
Posts: 1,609
#6: Mar 7 '08

re: Image Resize & Rotation


Quote:

Originally Posted by infoseekar

A BIG Thanks to You.. I was working on this from last week.. Thanks very much..

You are welcome.
Quote:

Originally Posted by infoseekar

Just one more question.. If I want to resize and rotate .jpg file, so I just replaced .png with .jpg, gif and so on.

No, it won't work. You will have to first check which image it is, then apply the corrosponding imagecreatefromXXX() function.
[php]<?php
$srcsize = getimagesize('image.png');
switch ($srcsize[mime])
{
case ('image/gif'):
$src_img = imagecreatefromgif('image.gif');
break;
case ('image/png'):
$src_img = imagecreatefrompng('image.png');
break;
case ('image/jpeg'):
$src_img = imagecreatefromjpeg('image.jpg');
break;
default:
return;
}[/php]
* make sure if its imagecreatefromjpeg or imagecreatefromjpg
Member
 
Join Date: Mar 2008
Posts: 34
#7: Mar 7 '08

re: Image Resize & Rotation


Quote:

Originally Posted by hsriat

You are welcome.

No, it won't work. You will have to first check which image it is, then apply the corrosponding imagecreatefromXXX() function.
[php]<?php
$srcsize = getimagesize('image.png');
switch ($srcsize[mime])
{
case ('image/gif'):
$src_img = imagecreatefromgif('image.gif');
break;
case ('image/png'):
$src_img = imagecreatefrompng('image.png');
break;
case ('image/jpeg'):
$src_img = imagecreatefromjpeg('image.jpg');
break;
default:
return;
}[/php]
* make sure if its imagecreatefromjpeg or imagecreatefromjpg

once again thank you.. i will give it a go
Member
 
Join Date: Mar 2008
Posts: 34
#8: Mar 10 '08

re: Image Resize & Rotation


Quote:

Originally Posted by infoseekar

once again thank you.. i will give it a go

Thanks for all your help

Regards

InfoSeekar
Reply