Resize Images in PHP Without Using Third-Party Libraries

Resize images in PHP without using third-party libraries?

Finally, I've discovered a way that fit my needs. The following snippet will resize an image to the specified width, automatically calculating the height in order to keep the proportion.

$image = $_FILES["image"]["tmp_name"];
$resizedDestination = $uploadDirectory.md5($randomNumber.$filename)."_RESIZED.jpg";

copy($_FILES, $resizedDestination);

$imageSize = getImageSize($image);
$imageWidth = $imageSize[0];
$imageHeight = $imageSize[1];

$DESIRED_WIDTH = 100;
$proportionalHeight = round(($DESIRED_WIDTH * $imageHeight) / $imageWidth);

$originalImage = imageCreateFromJPEG($image);

$resizedImage = imageCreateTrueColor($DESIRED_WIDTH, $proportionalHeight);

imageCopyResampled($images_fin, $originalImage, 0, 0, 0, 0, $DESIRED_WIDTH+1, $proportionalHeight+1, $imageWidth, $imageHeight);
imageJPEG($resizedImage, $resizedDestination);

imageDestroy($originalImage);
imageDestroy($resizedImage);

To anyone else seeking a complete example, create two files:

<!-- send.html -->

<html>

<head>

<title>Simple File Upload</title>

</head>

<body>

<center>

<div style="margin-top:50px; padding:20px; border:1px solid #CECECE;">

Select an image.

<br/>
<br/>

<form action="receive.php" enctype="multipart/form-data" method="post">
<input type="file" name="image" size="40">
<input type="submit" value="Send">
</form>

</div>

</center>

</body>

<?php

// receive.php

$randomNumber = rand(0, 99999);
$uploadDirectory = "images/";
$filename = basename($_FILES['file_contents']['name']);
$destination = $uploadDirectory.md5($randomNumber.$filename).".jpg";

echo "File path:".$filePath."<br/>";

if (is_uploaded_file($_FILES["image"]["tmp_name"]) == true) {

echo "File successfully received through HTTP POST.<br/>";

// Validate the file size, accept files under 5 MB (~5e+6 bytes).

if ($_FILES['image']['size'] <= 5000000) {

echo "File size: ".$_FILES["image"]["size"]." bytes.<br/>";

// Resize and save the image.

$image = $_FILES["image"]["tmp_name"];
$resizedDestination = $uploadDirectory.md5($randomNumber.$filename)."_RESIZED.jpg";

copy($_FILES, $resizedDestination);

$imageSize = getImageSize($image);
$imageWidth = $imageSize[0];
$imageHeight = $imageSize[1];

$DESIRED_WIDTH = 100;
$proportionalHeight = round(($DESIRED_WIDTH * $imageHeight) / $imageWidth);

$originalImage = imageCreateFromJPEG($image);

$resizedImage = imageCreateTrueColor($DESIRED_WIDTH, $proportionalHeight);

imageCopyResampled($images_fin, $originalImage, 0, 0, 0, 0, $DESIRED_WIDTH+1, $proportionalHeight+1, $imageWidth, $imageHeight);
imageJPEG($resizedImage, $resizedDestination);

imageDestroy($originalImage);
imageDestroy($resizedImage);

// Save the original image.

if (move_uploaded_file($_FILES['image']['tmp_name'], $destination) == true) {

echo "Copied the original file to the specified destination.<br/>";

}

}

}

?>

resize image without using image_lib in codeigniter

you don't have to download and configure 3rd party image_resize libraries when CI provides inbuilt libraries for that!!....If you want to do that just follow below steps:

STEP 1: import library using this single line...

$this->load->library('image_lib');

STEP 2:As per your code now,make changes as below

    public function fileUpload()
{

$title = $this->input->post('title');
$sourcePath = $_FILES["attachment_file"]['tmp_name']; // source path of the file;
$new_img = time(). '_' .$_FILES["attachment_file"]['name'];
$targetPath = 'images/header/' . $new_img; // Target path where file is to be stored
move_uploaded_file($sourcePath, $targetPath); // Moving Uploaded file

$config_resize['image_library'] = 'gd2';
$config_resize['create_thumb'] = TRUE;
$config_resize['maintain_ratio'] = TRUE;
$config_resize['master_dim'] = 'height';
$config_resize['quality'] = "100%";
$config_resize['source_image'] = $targetPath;
$config_resize['height'] = 60;
$config_resize['width'] = 60;
$config_resize['thumb_marker'] = '';
$config_resize['new_image'] = 'images/header/' . $new_img;
$this->image_lib->initialize($config_resize);
$this->image_lib->resize();

$uploaded = TRUE;

$this->person->save($title, $new_img ,'slider');

}

STEP 3: Thats all...it may require little modification as per your need.If any issue or you don't have any idea how the library works visit this link..

Official ellislab guideline

Resize image on server

I wasn't creating a file to the server's dir so this is what I did
move_uploaded_file($_FILES[$str]['tmp_name'], $target);
scale_image($target,$target);

Now the function scale_image()

function scale_image($image,$target)
{
if(!empty($image)) //the image to be uploaded is a JPG I already checked this
{
$source_image = imagecreatefromjpeg($image);
$source_imagex = imagesx($source_image);
$source_imagey = imagesy($source_image);

$dest_imagex = 300;
$dest_imagey = 200;

$image2 = imagecreatetruecolor($dest_imagex, $dest_imagey);
imagecopyresampled($image2, $source_image, 0, 0, 0, 0,
$dest_imagex, $dest_imagey, $source_imagex, $source_imagey);

imagejpeg($image2, $target, 100);

}
}

Thank you all very much, the resource you gave me helped me to create this function.

Resizing image without actually changing it in the folder

It would be a lot of hassle to save a temporary image etc. Why don't you just display it in HTML using the desired width?

<img src="someimage.extension" class="thumbnail"/>
<style>
.thumbnail {
width: 50px;
}
</style>

How can I resize an image from URL using PHP?

here is the solution based on imagecopyresampled (GD library) http://php.net/manual/en/function.imagecopyresampled.php

$content = file_get_contents('http://www.joomlaworks.net/images/demos/galleries/abstract/7.jpg');
$name = "http://www.joomlaworks.net/images/demos/galleries/abstract/7.jpg";
$parts = explode('.', $name);
$new_url = rand(0, pow(10, 5)) . '_' . time() . '.' . $parts[count($parts) - 1];
file_put_contents(DIRECTORY.'/' . $new_url , $content);

resizeImage($new_url, DIRECTORY.'/1_' . $new_url, 100, 100);
resizeImage($new_url, DIRECTORY.'/2_' . $new_url, 200, 200);
resizeImage($new_url, DIRECTORY.'/3_' . $new_url, 300, 300);

function resizeImage($source, $dest, $new_width, $new_height)
{
list($width, $height) = getimagesize($source);
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($source);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($image_p, $dest, 100);
}


Related Topics



Leave a reply



Submit