Jump to content

Inserting my funtion to rotate an image


The Little Guy

Recommended Posts

This code works correctly when it is all put together, except for the image size, it should resize to 50%, but when I add in my function, it doesn't resize it, but rotating it works. If I remove the function, then resizing does work, so where and how would I insert it?

OK... I have this code:

[code]<?php
// The file
session_start();
$img_id = $_GET['image_id'];
include"db.php";
$img = mysql_query("SELECT * FROM files where file_id='$img_id'")or die(mysql_query());
$imgs = mysql_fetch_array($img);

$filename = "users/".$_SESSION['user']."/$imgs[file_name]";
#$filename = "users/ryan/screen2.jpg";
//$filename = 'images/user_images/geoff.jpg';

$percent = .5;

function getext($file) {
$pos = strrpos($file,'.');
$str = substr($file, $pos);
return strtolower($str);
}

// Content type
if(getext($filename)=='.jpg'){
header('Content-type: image/jpeg');
}elseif(getext($filename)=='.gif'){
header('Content-type: image/gif');
}elseif(getext($filename)=='.png'){
header('Content-type: image/png');
}

// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;

// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);

if(getext($filename)=='.jpg'){
$source = imagecreatefromjpeg($filename);
#$source = rotate($filename,$_GET['degrees']);
//$thumb = rotate($source,$_GET['degrees']);
}elseif(getext($filename)=='.gif'){
$source = imagecreatefromgif($filename);
}elseif(getext($filename)=='.png'){
$source = imagecreatefrompng($filename);
imageAlphaBlending($source, true);
imageSaveAlpha($source, true);
}
// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Output
if(getext($filename)=='.jpg'){
imagejpeg($thumb);
}elseif(getext($filename)=='.gif'){
imagegif($thumb);
}elseif(getext($filename)=='.png'){
imagepng($thumb);
}
?>[/code]



I want to add this function, so I add it to the top.
[code]function rotate($filerotate,$rotationamount){
// File and rotation
$filename = $filerotate;
$degrees = $rotationamount;

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

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

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

// Output
return imagejpeg($rotate);
}[/code]


My question is... Where do I add this:
[b]$thumb = rotate($source,$_GET['degrees']);[/b]

I don't know if [b]$source[/b] or [b]$thumb[/b] is the correct variable to use, but [b]$_GET['degrees'][/b] is required
Link to comment
https://forums.phpfreaks.com/topic/31451-inserting-my-funtion-to-rotate-an-image/
Share on other sites

Maybe this will help...
I would like to combine these two scripts:

[code]<?php
// File and rotation
$filename = 'test.jpg';
$degrees = 180;

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

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

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

// Output
imagejpeg($rotate);
?>[/code]


And this one:

[code]<?php
// File and new size
$filename = 'test.jpg';
$percent = 0.5;

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

// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;

// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);

// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Output
imagejpeg($thumb);
?>[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.