Jump to content

Correct Way To Com,press JPG With GD Library


Recommended Posts

Hello, what's the correct use of GD library to compress JPG images please? I am using this upload script with TinyMCE. See below :

I have this snippet but it doesn't compress the image:

		// Accept upload if there was no origin, or if it is an accepted origin
		// If image is JPEG compress with GD library
		$filetowrite = $imageFolder . $temp['name'];
		if (in_array(strtolower(pathinfo($temp['name'], PATHINFO_EXTENSION)), array("jpg", "jpeg"))) {
		imagejpeg($temp['tmp_name'], $filetowrite, 75);
		} else {
		move_uploaded_file($temp['tmp_name'], $filetowrite);
		}
<?php
	$accepted_origins = array("http://localhost", "http://127.0.0.1", "http://192.168.1.1", "http://example.com");

	$month = strtolower(date('M'));
	$year = date('Y');
	$newspath = "img/news/";

	if (!is_dir("$newspath/$year/$month")) {
    mkdir("$newspath/$year/$month", 0777, true);
}
	$imageFolder = "$newspath/$year/$month/";

	reset ($_FILES);
	$temp = current($_FILES);
	if (is_uploaded_file($temp['tmp_name'])){
		if (isset($_SERVER['HTTP_ORIGIN'])) {
			// same-origin requests won't set an origin. If the origin is set, it must be valid.
			if (in_array($_SERVER['HTTP_ORIGIN'], $accepted_origins)) {
				header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']);
			} else {
				header("HTTP/1.1 403 Origin Denied");
				return;
			}
		}

		/*
			If your script needs to receive cookies, set images_upload_credentials : true in
			the configuration and enable the following two headers.
		*/
		// header('Access-Control-Allow-Credentials: true');
		// header('P3P: CP="There is no P3P policy."');

		// Sanitize input
		if (preg_match("/([^\w\s\d\-_~,;:\[\]\(\).])|([\.]{2,})/", $temp['name'])) {
				header("HTTP/1.1 400 Invalid file name.");
				return;
		}

		// Verify extension
		if (!in_array(strtolower(pathinfo($temp['name'], PATHINFO_EXTENSION)), array("gif", "jpg", "jpeg", "png"))) {
				header("HTTP/1.1 400 Invalid extension.");
				return;
		}

		// Accept upload if there was no origin, or if it is an accepted origin
		// If image is JPEG compress with GD library
		$filetowrite = $imageFolder . $temp['name'];
		if (in_array(strtolower(pathinfo($temp['name'], PATHINFO_EXTENSION)), array("jpg", "jpeg"))) {
		imagejpeg($temp['tmp_name'], $filetowrite, 75);
		} else {
		move_uploaded_file($temp['tmp_name'], $filetowrite);
		}
		// Respond to the successful upload with JSON.
		// Use a location key to specify the path to the saved image resource.
		// { location : '/your/uploaded/image/file'}
		// echo json_encode(array('location' => $filetowrite));

		$passpath = $year.'/'.$month.'/'.$temp['name'];

		echo json_encode(array('location' => $passpath));
	} else {
		// Notify editor that the upload failed
		header("HTTP/1.1 500 Server Error");
	}
?>

 

Link to post
Share on other sites

If you know what JPEG compression is then I would expect you to know that JPEGs are already compressed.

If you're looking to vary the degree of compression then take a closer look at imagejpeg()'s third argument. The one you have set at 75.

Link to post
Share on other sites

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.