Note that there are some explanatory texts on larger screens.

plurals
  1. PODividing the image into three sizes in PHP
    primarykey
    data
    text
    <p>I am actually trying to make an image into three sizes and then storing them in the server folder with different names. I am getting this image data in <a href="http://en.wikipedia.org/wiki/Base64" rel="nofollow">Base64</a> encoded format through the mobile client side. Here is the code where I am trying to convert the image into three images.</p> <p>This is the PHP file where I am receiving data.</p> <pre><code>$uploadPath = 'C:/xampp/htdocs/OSFiles/of-images/images/'; if(!is_dir($uploadPath)) mkdir($uploadPath,true) or trigger_error("Can't Create Folder"); $file = tempnam($uploadPath, 'image'); $fp = fopen($file, 'wb'); fwrite($fp, $binary); //$binary is--&gt; I am reading the image in binary as I am getting the photo data in base64 encoded format through mobile client side. So I decoded it to binary. fclose($fp); $result1 = mysql_query("INSERT INTO oc_t_item_resource(fk_i_item_id, s_name,s_extension,s_content_type, s_path) VALUES('$lastid','$s_name','$extension','$s_content_type','$imagepath')"); $imagelastid = mysql_insert_id(); // Create normal size $normal_path = $path = $uploadPath . $imagelastid . '.jpg' ; $size = explode('x', '640x480') ; ImageResizer::fromFile($file)-&gt;resizeTo($size[0], $size[1])-&gt;saveToFile($path) ; // Create preview $path = $uploadPath . $imagelastid . '_preview.jpg' ; $size = explode('x', '480x340') ; ImageResizer::fromFile($file)-&gt;resizeTo($size[0], $size[1])-&gt;saveToFile($path) ; // Create thumbnail $path = $uploadPath . $imagelastid . '_thumbnail.jpg' ; $size = explode('x', '240x200') ; ImageResizer::fromFile($file)-&gt;resizeTo($size[0], $size[1])-&gt;saveToFile($path) ; </code></pre> <p>This is my <strong>ImageResizer</strong> class which is in ImageResizer.php file.</p> <pre><code>&lt;?php class ImageResizer { public static function fromFile($imagePath) { return new ImageResizer($imagePath); } private $im; private function __construct($imagePath) { if(!file_exists($imagePath)) throw new Exception("$imagePath does not exist!"); if(!is_readable($imagePath)) throw new Exception("$imagePath is not readable!"); if(filesize($imagePath)==0) throw new Exception("$imagePath is corrupt or broken!"); if(osc_use_imagick()) { $this-&gt;im = new Imagick($imagePath); } else { $content = file_get_contents($imagePath); $this-&gt;im = imagecreatefromstring($content); } return $this; } public function __destruct() { if(osc_use_imagick()) { $this-&gt;im-&gt;destroy(); } else { imagedestroy($this-&gt;im); } } public function resizeTo($width, $height) { if(osc_use_imagick()) { $bg = new Imagick(); $bg-&gt;newImage($width, $height, 'white'); $this-&gt;im-&gt;thumbnailImage($width, $height, true); $geometry = $this-&gt;im-&gt;getImageGeometry(); $x = ( $width - $geometry['width'] ) / 2; $y = ( $height - $geometry['height'] ) / 2; $bg-&gt;compositeImage( $this-&gt;im, imagick::COMPOSITE_OVER, $x, $y ); $this-&gt;im = $bg; } else { $w = imagesx($this-&gt;im); $h = imagesy($this-&gt;im); if(($w/$h)&gt;=($width/$height)) { //$newW = $width; $newW = ($w &gt; $width)? $width : $w; $newH = $h * ($newW / $w); } else { //$newH = $height; $newH = ($h &gt; $height)? $height : $h; $newW = $w * ($newH / $h); } $newIm = imagecreatetruecolor($width,$height);//$newW, $newH); imagealphablending($newIm, false); $colorTransparent = imagecolorallocatealpha($newIm, 255, 255, 255, 127); imagefill($newIm, 0, 0, $colorTransparent); imagesavealpha($newIm, true); imagecopyresampled($newIm, $this-&gt;im, (($width-$newW)/2), (($height-$newH)/2), 0, 0, $newW, $newH, $w, $h); imagedestroy($this-&gt;im); $this-&gt;im = $newIm; } return $this; } public function saveToFile($imagePath) { if(file_exists($imagePath) &amp;&amp; !is_writable($imagePath)) throw new Exception("$imagePath is not writable!"); if(osc_use_imagick()) { $this-&gt;im-&gt;setImageFileName($imagePath); $this-&gt;im-&gt;writeImage($imagePath); } else { imagejpeg($this-&gt;im, $imagePath); } } public function show() { header('Content-Disposition: Attachment;filename=image.jpg'); header('Content-type: image/jpg'); if(osc_use_imagick()) { } else { imagepng($this-&gt;im); } } } ?&gt; </code></pre> <p>The image is not getting stored at all in the images folder. Where am I going wrong? </p> <p>A code snippet with corrections would be appreciated.</p> <p>(I am new to <a href="http://en.wikipedia.org/wiki/PHP" rel="nofollow">PHP</a>, so please be gentle.)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload