Jump to content

Resize image


Kenny Pollock

Recommended Posts

I'm trying to read a directory, for each image, resize it, then add a watermark to it and then delete the original. It's not adding the watermark, and it's making an image inside an image...

 

<?php

$directory = 'show_coverage/';

$handler = opendir($directory);

while ($file = readdir($handler))
{
if ($file != '.' && $file != '..')
{
	if ( !is_dir( $directory . $file . '/' ) )
	{
		$width = '600';
		$datename = date("Y-m-d-G-i-s")."-".rand(0,100);
		$target_path = $directory . $datename . '.jpg'; 
		list($width_orig, $height_orig) = getimagesize( $directory . $file );

		$height = ($width / $width_orig) * $height_orig;
		$image = imagecreatetruecolor( $width, $height );
		$image = imagecreatefromjpeg( $directory . $file );
		imagecopyresampled( $image, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig );  
		imagejpeg($image, $target_path, 100);

$watermark = imagecreatefrompng('flminiswatermark.png');  
$watermark_width = imagesx($watermark); 
$watermark_height = imagesy($watermark);
$image2 = imagecreatetruecolor($watermark_width, $watermark_height);
$image2 = imagecreatefromjpeg($target_path);
$size = getimagesize($target_path);  
$dest_x = $size[0] - $watermark_width - 5;  
$dest_y = $size[1] - $watermark_height - 5;  
imagecopymerge($image2, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 100);  
imagejpeg($image2, $target_path, 100);  

		unlink( $directory . $file );
		echo '<img src="' . $directory . $datename . '.jpg" alt="" /><br /><br />';			
	}
}
}

closedir($handler);

?>

Link to comment
Share on other sites

Maybe its me but your code looks a bit messed up. A simple merging of two png's should be like:

 

$image = imagecreatefrompng('image.png');
$watermark = imagecreatefrompng('watermark.png');
imagecolortransparent($watermark, imagecolorat($image, 0, 0));
imagecopymerge($image, $watermark ,0 ,0 ,0 ,0 , 100, 50, 100); 
header('Content-type: image/png');
imagepng($image , "", 100);

 

I did this code from a tutorial some time a go and it worked greatly. Maybe u can consider its basic usage.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.