Jump to content

Recommended Posts

Hi, ok as far as im aware there is a method which enables you to reduce the file size of an image using PHP.  This is obviously great for creating smaller sized, fast loading thumbnails of a larger image without having to manipulate the image yourself at all.  Great for people who dont really know anything about image manipulation too!

 

Ok then so my question is: "Does anyone know how this is done!?"

 

Or can anyone suggest a better way to do this without having to manipulate the image yourself and then upload it to the server before it can be accessed.

 

cheers!

Link to comment
https://forums.phpfreaks.com/topic/51799-solved-reducing-an-image-file-size/
Share on other sites

Use imagecopyresampled

 

either edit the code to create a new file or use as is

 

 

image.php


<?php
// The file
$filename = $_GET['img'];
$percent = 0.5;

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

// Get new dimensions
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;

// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

// Output
imagejpeg($image_p, null, 100);
?> 

 

 

use as

 

<img src='image.php?img=test.jpg' />

Hi mate, ive tried what you suggested however the outputted image will not display, as if im calling an image which is not on the server (the white square with the red cross inside)???

 

ive also tried moving the whole image file inside my images folder and calling it from there, not working!

 

any suggestions?

other tests to try are

 

put test.jpg in the same folder and then try

http://mydomain.com/mysite/test/image.php?img=test.jpg

 

if this fails

 

comment the line

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

 

try again you may see alot of gook appear but at the top you may see a error with the file path

Unfortunately mate its on my localhost,

 

the first line of image.php is <? however?

 

here is exactly what i am doing:

 

image.php



<?
ob_start();
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>

<?php
// The file
$filename = $_GET['img'];
$percent = 0.5;

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

// Get new dimensions
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;

// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

// Output
imagejpeg($image_p, null, 100);
?> 

</body>
</html>

 

reduce_image.php

 


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>



<img src='image.php?img=banner.jpg' />



</body>
</html>

Ahh

 

no no

 

the whole page is

<?php
// The file
$filename = $_GET['img'];
$percent = 0.5;

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

// Get new dimensions
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;

// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

// Output
imagejpeg($image_p, null, 100);
?> 

 

Nothing more nothing less

 

 

remove the <html> etc etc etc

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.