change two images in php - php

EDIT:
I have to images (or two strings) on a page. One on the left and the other on the right side. I want them to randomly switch their position when refreshing the site. How can I do this? Thanks.

Try this
$image1 = echo "<img src='image_path'>";
$image2 = echo "<img src='image_path'>";
if(rand(0,1)==0){
$image3 = $image1;
$image1 = $image2;
$image2 = $image3;
}
<div style="float:right">$image1</div>
<div style="float:left">$image2</div>

Use imagerotate() and rand() functions.

You can use imagerotate for this thing. The documentation has this simple example:
<?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);
So, just write a script that takes a filename as a parameter (via $_GET) and then rotate it by a random degree (use rand for this). Then just change your image link to rotator.php?file=first.jpg.
Another option is to do the rotation with CSS3 and JavaScript. See this link for more information on rotating with CSS3.

Related

PHP Rotate Image from request file

I am now using the following method to store an image to server from input file type
$image = $request->file('file');
$filename = $item->itemId . '.png';
Storage::disk('s3')->put('/'.$filename, file_get_contents($image), 'public');
and I found method to rotate the image with PHP
$filename = 'test.jpg';
$degrees = 180;
header('Content-type: image/jpeg');
$source = imagecreatefromjpeg($filename);
$rotate = imagerotate($source, $degrees, 0);
imagejpeg($rotate);
but I dont know how implement the code with $request->file('file')
Thanks!
I am not sure where you are getting your functions from, imagecreatefromjpeg() and imagerotate(), but if you just use PHP's own, available functions (provided by Imagick), you can do something much simpler...
$image = new Imagick();
$image_filehandle = fopen('some/file.jpg', 'a+');
$image->readImageFile($image_filehandle );
$image->rotateImage("FFFFFF", 90); # Rotate 90 degrees, keep background of "FFFFFF" (white)
$image_icon_filehandle = fopen('some/file-rotated.jpg', 'a+');
$image->writeImageFile($image_icon_filehandle);
The background color ("FFFFFF") is applied here if the image rotates and leaves a certain amount of background (does not happen for rotate degrees in increments of 90).

PHP: how to use getimgasize() after the image is resized using getimageresized after upload?

using the example given at http://www.php.net/manual/en/function.imagecopyresized.php ... how to get image sizes afterward using getimagesize() function?
CODE:
<?php
if(isset($_FILES['images'])){
//TEST1:
$img = resize_this_image_now($_FILES['images']['tmp_name']);
//TEST2:
$img = resize_this_image_now($_FILES['images']['name']);/// This Drastically failed.
$new_image = getimagesize($img);
var_dump($new_image[0]);// I guessed this should have printed out the WIDTH_OF_THE_IMAGE... but, it prints some NON_READABLE stuffs (why?)
}
// The PHP.NET CODE in a Function
function resize_this_image_now($filename){
// 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
return imagejpeg($thumb);
}
?>
All I want is to get the Size of the Image.... also, is it possible to do something like:
$_FILES['images']['tmp_name'] = $the_newly_resized_image_returned_from_the_PHP_dot_NET_code'; .... So that the ['images']['tmp_name'] will now have as source this new image??
Any suggestion is highly appreciated...
I decided to spend sometime examining you question. What I found out is that, I don't think you'd need to return the resized image through imagejpeg() the way you did. You might also need to add a imagedestroy(), after you call imagejpeg() in you function, to destroy the temporary memory used.
You need to first fully Upload the Image before resizing it. If you'd like, you could send the image in a temporary storage while you do whatever you want to it so that Php does not have to deal with it in a 'tmp_name' format...Then You can destroy the image later on.
Once the image is fully uploaded, things become easier. The codes might look something like:
if(isset($_FILES['images'])){
//may be some random numbers to accompany it.
$rand = floor((mt_rand()+rand()+mt_rand())/3);
//Send it to the temporary folder you have had to create.
if(move_uploaded_file(
$_FILES['images']['tmp_name'],'temporary_storage/image_'.$rand.'.jpg')){
//Then run the `resize` function from here.
$image_resized = resize_this_image_now('temporary_storage/image_'.$rand.'.jpg');
//Now You can get the size if you wish.
list($width,$height) = getimagesize('temporary_storage/image_'.$rand.'.jpg');
// Out put
echo "W:".$width."<br>H:".$height;
//After you use it as desired, you can now destroy it using unlink or so.
unlink('temporary_storage/image_'.$rand.'.jpg');
}else{
echo "Upload Error goes here";
}
}
Note this answer is produced after several trials and errors... Please use this strategy wisely.
Hope it helps.

Image not rotating

I am trying to rotate image using php imagerotate function but its not working.
GD Library is also on.
i have tried this ,
public function rotate()
{
$targ_w = 240;
$targ_h = 180;
$jpeg_quality = 100;
$degrees = 90;
$src = "/photos/sunset.jpg";
$image = imagecreatefromjpeg($src);
$rotatedImage = imagerotate($image,$degrees,0);
imagejpeg( $rotatedImage,$src,$jpeg_quality);
imagedestroy($rotatedImage);
die();
}
<?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);
// Free the memory
imagedestroy($source);
imagedestroy($rotate);
?>
You're outputing the unchanged $image to file. You should output the rotated one.
imagejpeg( $rotatedImage,$name ,$jpeg_quality);
The second thing - your image is empty. It has only defined width and height but has no content inside it. You defined a $src variable but you don't use it at all.
Maybe you want to replace imagecreatetruecolor with this:
$src = "/photos/sunset.jpg";
$image = imagecreatefromjpeg($src);
You must output the rotated image (pass $rotatedImage instead of $image):
$rotatedImage = imagerotate($image,$degrees,0);
header('Content-type: image/jpeg'); //Header is required to output the image.
imagejpeg($rotatedImage,$name ,$jpeg_quality);
imagedestroy($rotatedImage);
die();
If you are trying to show the image then you need to change that:
header('Content-type: image/jpeg'); //Add jpeg header
imagejpeg( $rotatedImage, NULL, 100); //<-- Notice i remove the $src parameter
If you want to update your jpg file then your code will work, but the user that runs the php file need permissions to write the file. Of course your current image will be overwritten.
And as i said in comments you will need GD version 1.8 or later to work with jpeg files according to php.net

PHP Rotate image file permanently

I want a user to be able to permanently rotate an image file clockwise or counter clockwise. I have tried imagejpeg($rotate) but can't seem to make it work right.
<form method="GET" action="rotate.php">
Rotate:<input type="radio" name="rotate" value="clockwise">Clockwise
<input type="radio" name="rotate" value="counterclockwise">Counter clockwise
<input type="Submit" name="Submit1"/>
</form>
I am trying to allow the user to be able to choose the radio button direction and click "Submit". Then the displayed image will update rotated to whichever direction they chose and stay that way permanently whenever it is used again. Any help or direction?
<img src=\"uploads/$user/$folder/$image\"/></a>";
use imagerotate() this will rotate image permanently.
<?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);
?>
http://php.net/manual/en/function.imagerotate.php
Or you can use this using jquery
http://www.linein.org/examples/jquery_rotate/
If you want to rotate your picture permanently, HTML or CSS is not the way to go. You will need some server-side scripting to store the rotated picture.
You sould have a look at GD library. For this kind of image manipulation, I have been using Imagine library, which is a library based on GD.
If someone else ever needs this. Here is a function that will replace the original with the rotated image:
public function autoRatateImage($src, $exifCode = ''){
if($exifCode == ''){
$exif = exif_read_data($src);
}else{
$exif['Orientation'] = $exifCode;
}
$source = imagecreatefromjpeg($src);
if (!empty($exif['Orientation'])) {
switch ($exif['Orientation']) {
case 3:
$image = imagerotate($source, 180, 0);
break;
case 6:
$image = imagerotate($source, -90, 0);
break;
case 8:
$image = imagerotate($source, 90, 0);
break;
}
if (file_exists($src)) {
unlink($src);
}
imagejpeg($image, $src , 100);
}
}

Passing an Variable from one PHP File to another

I'm working on an image resizer, to create thumbnails for my page. The resizer works on principle of include a DIRECT link to the image. But what I want to do is put in the PHP Variable in the URL string, so that it points to that file and resizes it accordingly.
My code is as follows :
<img src="thumbnail.php?image=<?php echo $row_select_property['image_url']; ?>
Image Resize :
<?php
// Resize Image To A Thumbnail
// The file you are resizing
$image = '$_GET[image_url]';
//This will set our output to 45% of the original size
$size = 0.45;
// This sets it to a .jpg, but you can change this to png or gif
header('Content-type: image/jpeg');
// Setting the resize parameters
list($width, $height) = getimagesize($image);
$modwidth = $width * $size;
$modheight = $height * $size;
// Creating the Canvas
$tn= imagecreatetruecolor($modwidth, $modheight);
$source = imagecreatefromjpeg($image);
// Resizing our image to fit the canvas
imagecopyresized($tn, $source, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);
// Outputs a jpg image, you could change this to gif or png if needed
imagejpeg($tn);
?>
What I am trying to do is pass on the variable "image=" to the Thumbnail script. At the moment I am passing it through the URL string, but it doesnt seem to load the graphic.
I'll try expand on this more, should you have questions as I am finding it a little difficult to explain.
Thanks in advance.
I suspect at least part of the problem is that your existing...
$image = '$_GET[image_url]';
...line is creating a text string, rather than getting the contents of the 'image_url' query string. Additionally, your passing in the image name as "?image=" in the query string, so you should simply use "image", not "image_url".
As such, changing this to...
$image = $_GET['image'];
...should at least move things along.
Change it
$image = '$_GET[image_url]';
to
$image = $_GET['image'];
$image = '$_GET[image_url]';
should be
$image = $_GET['image'];

Categories